• edited by
4,158 views
4 4 votes

Consider the following code executed on an n-element array A such that each element i an A satisfies the
condition 0 ≤ A[i] < 1. The code also uses an auxiliary array, B[0 to n – 1].
Random-Sort (A)
{
1. n ← Length [A]
2. For i ← 1 to n
3. do insert A[i] into list B [ ⌊n A[i]⌋ ]
4. For i to n – 1
5. do sort list B[i] with
6. Concatenate the list B[0], B[1], ..., B[n – 1] together in order.
}
Which algorithm will be placed in the blank at line 5 for sorting (stable) so that the code will give minimum
time complexity?

1 Answer

Best answer
7 7 votes

See this line ""Concatenate the list B[0], B[1], ..., B[n – 1] together in order"".

This Algorithm is implementing a bucket sort, in which the input is divided into fixed length buckets. 

Take for example => 34,109,349,233,567,895,234,456,245,.......

Now this input size can be divided into fixed length buckets of size 100 each and each bucket is sorted particularly, then as the last line suggests, concatenate all the buckets. Main thing lies that which algorithm are we going to use to sort a particular bucket.

Best case lies, when elements are uniformly distributed between the buckets.

Worst case lies, when all the elements are multiplexed into a single bucket. So, for a large input, sorting algorithm for this one bucket can lead to worst case sorting.

Bucket sort always uses stable sorting algorithm for sorting a particular bucket as a subroutine.

There are stable sorting algorithms available => Merge sort , Insertion sort , Bubble sort .

A). Merge sort Worst case O(N log N).

B). Insertion sort Worst case O(N2).

C). Bubble sort Worst case O(N2).

Now, stable sorting algorithm gives minimum WC complexity, is Merge sort.

• selected by
Position:
Show:

Related questions

3 3 votes
4 4 answers
2.5k
2.5k views
newdreamz a1-z0 asked Jan 21, 2019
2,531 views
Consider a scenario of modified quick sort, where we have given an input sorted array A[1 .. . n], all elements of array are distinct and n >=3. Pivot is the median of se...
4 4 votes
4 answers 4 answers
5.0k
5.0k views
Ramij asked Dec 20, 2018
4,960 views
Suppose there are 4 sorted list of 16 elements each. If we merge these lists into a single sorted list of 64 elements. The key comparisons that are needed in the worst ca...
0 0 votes
0 0 answers
984
984 views
Abhishek Kumar 38 asked Dec 19, 2018
984 views
Which of the following sorting algorithm represented by above code?
0 0 votes
1 1 answer
5.1k
5.1k views
Rajat Agrawal007 asked Dec 17, 2018
5,140 views
Which of the following input will give best case time for selection sort?(A) 1 2 3 4 5 6 7 8 9 10(B) 2 3 1 5 9 7 8 6 10(C) 10 9 8 7 6 5 4 3 2 1 (D) All of above take same...