retagged by
43,869 views
19 votes
19 votes

The minimum number of comparisons required to sort 5 elements is -

  1. 4
  2. 5
  3. 6
  4. 7
retagged by

8 Answers

0 votes
0 votes

Can be done using insertion sort. Insertion sort takes n2/4 comparisons in average case so answert would be 7 but since here we are not given average/worst case, so go with Counting sort.

0 votes
0 votes
By using merge sort, the minimum no of comparison is equal to ceiling logn!.

Here n=5. So minimum number of comparison =7
0 votes
0 votes

There are 5 elements. 

You need to ask yourself the possible combinations of these 5 elements. 

eg. Let's say there are just 2 elements(3,4) then what are the possible arrangements 

  • 3, 4
  • 4, 3

One of them is sorted say (3, 4)

Similarly for 5 elements (1,2,3,4,5) is the sorted arrangement but what are the other possible such patterns

few of them are 

  • 2,3,4,5,1
  • 1,2,4,5,3
  • 4,1,3,2,5
  • 5,4,3,1,2

1st number, 2nd number, 3rd number, 4th number, 5th number

for 1st number I can pick any of the 5 numbers from 1,2,3,4,5 -> picked 5 (1,2,3,4) left

for 2nd number I can pick any of the 4 numbers remaining from 1,2,3,4 -> picked 2 , (1,3,4) left

for 3rd number I can pick any of the 3 numbers remaining from 1,3,4 -> picked 4 , (1,3) left

for 4th number I can pick any of the 2 numbers remaining from 1,3 -> picked 1 , (3) left

for 5th number I can only pick 3

Turns out there are 5! such arrangements that is 120

let's say the arrangement

  • 1,2,4,3,5 is represented as 1 in binary
  • 1,2,3,5,4 is represented as 10 in binary 
  • 1,3,2,5,4 is represented as 11 in binary
  • 1,4,5,3,2 is represented as 100 in binary
  • .
  • .
  • .
  • 1,2,3,4,5 is represented as 1111000 in binary

so we would need 7 binary bits to represent all the patterns and one of them is our desired sorted arrangement.

If there is a checker who verifies our given output with the binary representation then she would have to check 7 bits. 

if there is no checker then we can output the given number as it is and we can argue that our output is correct since no one is going to check. 

 

//now how would you make sure to output the correct answer every time. 

You can act as a checker and with every bit you check you are eliminating half of the possible arrangements (2^7/2) after you check 7 bits you would left with just 1 arrangement and that is your answer. 

 

That's how you get log(120) 

 

Related questions

1.4k
views
1 answers
2 votes
yes asked Oct 6, 2015
1,439 views
for example array contain a[1 2 3 3 3 3 3 4 5] then retun(1)
1.2k
views
2 answers
0 votes
dhruba asked Jun 5, 2023
1,243 views
Binary search is performed on a sorted array of n elements. The search key is not in the array and falls between the elements at positions m and m+1 (where 1 ≤ m < n). ...
1.9k
views
1 answers
0 votes
Siddhi Viradiya asked Apr 3, 2016
1,885 views
i cannot understand the following explanation..how solution is (3/2)n-2???If n is a power of 2, then we can write T(n) as:T(n) = 2T(n/2) + 2After solving above recursion,...
877
views
1 answers
1 votes
iarnav asked May 4, 2019
877 views
I’ve seen this wikipedia article – https://en.wikipedia.org/wiki/Comparison_sortAlso see this link – https://gateoverflow.in/32948/minimum-number-of-comparisonshttp...