Searching and Sorting
Searching and Sorting
Searching and Sorting
searching: linear and binary. Binary Search is generally more efficient than Linear
Search, especially for large datasets. Linear Search has a time complexity of O(n),
while Binary Search has a time complexity of O(log n). Binary Search requires
sorted data, whereas Linear Search does not.
Linear:
public static int liner(int arr[], int x)
{
for (int i = 0; i < arr.length; i++) {
if (arr[i] == x)
return i;
}
return -1;
}
binary code:
int binarySearch(int arr[], int x)
{
int low = 0, high = arr.length - 1;
while (low <= high) {
int mid = low + (high - low) / 2;
if (arr[mid] == x)
return mid;
if (arr[mid] < x)
low = mid + 1;
else
high = mid - 1;
}
return -1;
}
recursively:
1. **Selection Sort**:
- **Concept**: Repeatedly finds the smallest element from the unsorted portion
and swaps it with the first unsorted element.
- **Performance**: \(O(n^2)\) time complexity; simple but inefficient for large
datasets.
2. **Bubble Sort**:
- **Concept**: Repeatedly steps through the list, compares adjacent elements,
and swaps them if they're in the wrong order, "bubbling" the largest element to the
end.
- **Performance**: \(O(n^2)\) time complexity; inefficient for large datasets,
but easy to implement.
3. **Insertion Sort**:
- **Concept**: Builds a sorted portion of the list by repeatedly taking the next
element and inserting it into its correct position.
- **Performance**: \(O(n^2)\) time complexity; efficient for small datasets or
nearly sorted data.
4. **Merge Sort**:
- **Concept**: Divides the list into halves, recursively sorts them, and then
merges the sorted halves back together.
- **Performance**: \(O(nlog n)\) time complexity; efficient and stable, good for
large datasets.
5. **Quick Sort**:
- **Concept**: Selects a "pivot" element, partitions the array into elements
less than and greater than the pivot, and recursively sorts the partitions.
- **Performance**: Average \(O(nlog n)\) time complexity; efficient but can
degrade to \(O(n^2)\) in the worst case, though this can be mitigated with good
pivot selection.
-----------------------------------------------------------
selection sort:
// Function call
quickSort(arr, 0, N - 1);
System.out.println("Sorted array:");
printArr(arr);
}
-------------------------------------------------------------------------
time complexity:
https://www.youtube.com/watch?v=9TlHvipP5yA
https://www.youtube.com/watch?v=5v-tKX2uRAk
1<logn<root n<n<nlogn<n2<n3...2^n<3^n...<n^n
Quick Sort:
Average-Case Time Complexity: O(n log n)
Worst-Case Time Complexity: O(n^2) (though this is rare with good pivot selection)
Performance: Typically the fastest in practice for large datasets, especially with
a good pivot selection strategy.
Merge Sort:
Average-Case Time Complexity: O(n log n)
Worst-Case Time Complexity: O(n log n)
Performance: Very reliable and consistent; often preferred for large datasets and
in applications requiring stable sorting.
Insertion Sort:
Average-Case Time Complexity: O(n^2)
Best-Case Time Complexity: O(n) (when the array is nearly sorted)
Performance: Fast for small or nearly sorted datasets, but not efficient for large
datasets.
Selection Sort:
Average-Case Time Complexity: O(n^2)
Worst-Case Time Complexity: O(n^2)
Performance: Simple to implement but generally slower than other algorithms on
large datasets.
Bubble Sort:
Average-Case Time Complexity: O(n^2)
Worst-Case Time Complexity: O(n^2)
Performance: Typically the slowest of the five, mainly used for educational
purposes or small datasets.
Summary:
Quick Sort is generally the fastest for large datasets.
Merge Sort is very efficient and reliable, especially when stable sorting is
required.
Insertion Sort can be the fastest for small or nearly sorted datasets.
Selection Sort and Bubble Sort are slower and generally not preferred for large
datasets.
-----------------------------------------------------------------------------------
----------------------------------
time complexity of searching algorithms: