edited by
1,354 views
2 votes
2 votes

A list of integers is almost sorted with only the largest number being out of place. If this information is not known to the algorithm, then which of the following algorithms can sort the list the fastest?

  1. Bubble sort
  2. Selection sort
  3. Insertion sort
  4. Shell sort
edited by

2 Answers

Best answer
5 votes
5 votes

If integers are nearly or almost sorted , then insertion sort is the best. 

If we race all the four algorithms, then clearly insertion sort is the winner . Insertion sort provides a O(n2) worst case algorithm that adapts to O(n) time when the data is nearly or almost sorted. 

  • Bubble sort is fast and comes second bcoz insertion sort has lower overhead and even if completely sorted , bubble sort again traverses all the elements to know that no swaps are needed which has high overhead.
  •  
  • Shell sort is fast and comes third because it is based on insertion sort.
  • Selection sort is not good for this case and is very slow .
  • Merge sort, heap sort, and quick sort do not even adapt to nearly sorted data.
  • Time complexity ===> 

Insertion sort --> O(n) and it will take some 1 pass 

Bubble sort -->  O(n) and it will take some 2 pass or can be more (1 for actual swapping and last pass for again checking that this time no pass occurs which is overhead) .

Shell sort ---> O(nlogn)

Selection sort --> O(n​)...

selected by
0 votes
0 votes

Its answer is C.Insertion Sort, Property of Insertion sort is it give best case time complexity of O(n) when all elements are almost sorted. 

Related questions

8 votes
8 votes
2 answers
2
0 votes
0 votes
1 answer
3
soorajchn asked Mar 14, 2015
5,592 views
a) n- ( lg(n)) - 2b) n + (lg(n)-2)
2 votes
2 votes
1 answer
4
yes asked Oct 6, 2015
1,431 views
for example array contain a[1 2 3 3 3 3 3 4 5] then retun(1)