Linear Search, Binary Search
Bubble Sort, Merge Sort
Quick Sort, Selection Sort
Insertion Sort, Radix Sort, Bucket Sort
Complexity Issues of algorithms
Linear Search is defined as a sequential search algorithm that starts at one end and goes through each element of a list until the desired element is found, otherwise the search continues till the end of the data set. It is the easiest searching algorithm.
In Binary search, best case occurs when the element to search is found in first comparison, i.e., when the first middle element itself is the element to be searched. The best-case time complexity of Binary search is O(1).
Average Case :
The average case time complexity of Binary search is O(logn)
Worst Case:
In Binary search, the worst case occurs, when we have to keep reducing the search space till it has only one element. The worst-case time complexity of Binary search is O(logn).
Traverse a collection of elements
Move from the front to the end
“Bubble” the largest value to the end using pair-wise comparisons and swapping
Algorithm Bubble_Sort(a, n)
{
for i := 1 to n – 1 do
{
// to keep track of the number of iterations
for j := 0 to n –i-1
{
// to compare the elements inside the particular iteration
if (a[j] > a[j + 1]) then
swap(a[j], a[j + 1]); // swap if any element is greater than its adjacent element
}
}
}
Best Case: The best case occurs when the array is already sorted. So the number of comparisons required is N-1 and the number of swaps required = 0. Hence the best case complexity is O(N).
Worst Case: The worst-case condition for bubble sort occurs when elements of the array are arranged in decreasing order.In the worst case, the total number of iterations or passes required to sort a given array is (N-1). where ‘N’ is the number of elements present in the array.
In each iteration Total number of swaps = Total number of comparison
Total number of comparison (Worst case) = N(N-1)/2Total number of swaps (Worst case) = N(N-1)/2
So worst case and average case time complexity is O(N2) as N2 is the highest order term.
Partitioning a[1:n] into two subarrays in such a way that sorted subarrays do not need to be merged later.
This is accomplished by rearranging the elements in a[1:n] such that a[i] <= a[j] for all i between 1 to m, and for all j between m+1 to n where 1<=m<=n.
Thus, the elements in a[1:m] and a[m+1:n] can be sorted independently
No need for merging
The rearrangement of the elements is accomplished by picking some element of a[],say t = a[s],and then reordering the other elements so that all elements appearing before t in a[1:n] are less than or equal to t and all elements appearing after t are greater than or equal to t.
This rearranging is referred to as partitioning.
In selection sort, the smallest value among the unsorted elements of the array is selected in every pass and inserted to its appropriate position into the array.
In this algorithm, the array is divided into two parts, first is sorted part, and ano