in Algorithms retagged by
12,059 views
14 votes
14 votes

Consider the following array.$$\begin{array}{|l|l|l|l|l|l|} \hline 23&32&45&69&72&73&89&97 \\ \hline\end{array}$$ Which algorithm out of the following options uses the least number of comparisons (among the array elements) to sort the above array in ascending order?

  1. Selection sort
  2. Mergesort
  3. Insertion sort
  4. Quicksort using the last element as pivot
in Algorithms retagged by
by
12.1k views

2 Comments

No algorithm can beat insertion sort, if array is already sorted
6
6
@ashwani

If array is already sorted, Bubble sort also takes $O(n)$ comparisons then why insertion sort is better than bubble sort?
3
3

6 Answers

26 votes
26 votes
Best answer

Correct Option : C (Insertion Sort)
Explanation:
The given array is already sorted in ascending order. 
For already sorted array, different sorting algorithms behave as following :

Selection sort :
No matter how the data is arranged, there would always be comparisons and swaps made and so the time complexity for best,average and worst case is $:O(n^2).$
In first pass, we need $n-1$ comparisons (Or $n$ comparisons, depending on the implementation) 
In second pass, we need $n-2$ comparisons (Or $n-1$ comparisons, depending on the implementation) and so on.

So, the number of comparisons required by a selection sort of n items can be computed by the formula:
$(n-1) + (n-2) + \ldots + 1 = (n)(n-1)/ 2$

Or

Number of selection sort comparisons  $= (n+1)(n)/ 2$

Basically, number of comparisons is $Θ(n^2)$ in all cases. 


Insertion Sort :
When elements are already sorted in desired order, there are no swaps and the correct position of the element in the sorted list is the current index itself. The time complexity is $:O(n)$
Number of comparisons $= n-1$ 
Comparisons in total $:1 + 1 + \ldots + 1 = n − 1 \in Θ(n).$

Merge Sort :
We are dividing the list into two halves, no matter if the list is sorted or not. But if the array is sorted, while merging the list, there are no swaps merging results into an array itself. Thus, the best, average and worst case time complexity is$: O(n\log n)$

Number of comparisons, in all cases, will be $O(n\log n)$

Quick Sort :
If we use the last or first element as pivot, then Quick Sort will give worst case performance if the elements are in a sorted or reverse sorted manner. So, for the given array in the question, Quick Sort will behave in worst manner and will take $O(n^2)$ comparisons.

The best and average case time complexity is $:O(n\log n)$
Number of comparisons, in best case, will be $O(n\log n)$

So, answer will be insertion sort. 

edited by

3 Comments

But number of comparisons in the best case in merge sort is (n/2) isn’t right?
0
0

@gleise_21 

But number of comparisons in the best case in merge sort is (n/2) isn’t right?

This “(n/2) comparisons in best case” is for final merge step of Merge sort, Not for entire merge sort. Merge sort takes, in every case, $O(nlogn)$ comparisons for $n$ elements.

Merge sort’s best case is when the largest element of one sorted sub-list is smaller than the first element of its opposing sub-list, for every merge step that occurs. Only one element from the opposing list is compared, which reduces the number of comparisons in each merge step to N/2.

5
5
So, it will take O(nxlgn) only irrespective of worst case or best case? Thanks for telling me about of (n/2) dilemma
0
0
0 votes
0 votes
  1. Insertion sort   (Best case time complexity of insertion sort is linear )
0 votes
0 votes
The array is already sorted in desired order, insertion sort will do least number of comparison in this case.
0 votes
0 votes

If the array is already sorted in ascending order then the no of comparisions will be n-1 which is the  best among all the comparison bound sorting algorithm,

Hence ans is C.

Answer:

Related questions