retagged by
12,335 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
retagged by

6 Answers

Best answer
26 votes
26 votes

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
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

17 votes
17 votes
4 answers
1
Arjun asked Feb 18, 2021
10,167 views
Consider the following three functions.$$f_1=10^n\quad f_2=n^{\log n}\quad f_3=n^{\sqrt {n}}$$Which one of the following options arranges the functions in the increasing ...
8 votes
8 votes
3 answers
2
Arjun asked Feb 18, 2021
10,852 views
Consider the following undirected graph with edge weights as shown:The number of minimum-weight spanning trees of the graph is ___________.