Consider the list below. What happens to the number of comparisons for each of the sort algorithms if the list is already sorted.
3 8 12 34 54 84 91 110
The processing of selection and bubble sort, as written, is independent of the original configuration of the items in the list, and therefore remain O(n2). But given that the data is originally sorted, the inner loop of the insertion sort will only make one comparison for each iteration, giving this situation the best case efficiency of O(n).
Because the quick sort algorithm, as written, chooses the first element in the partition as the partition element, each recursive step will only handle one element for an initially sorted list, and the efficiency degrades to the worst case scenario of O(n2). The merge sort guarantees equal partitions, and is O(n log n) in all cases, including this situation in which the elements are initially sorted.
You might also like to view...
On Facebook, you can find photos where a given person appears by searching for that person's tag
Indicate whether the statement is true or false
Windows 8.1 provides the Compatibility mode that assists you in troubleshooting applications that do not run properly in Windows 8.1
Indicate whether the statement is true or false