GfG QA is closing soon... Please refer PRACTICE or GfG now on wards !!

Efficent algorithm to sort n numbers with many duplicates ( distinct integers O(logn)).

We seek to sort a sequence S of n integers with many duplications, such that
the number of distinct integers in S is O(log n). Give an O(n log log n) worst-case
time algorithm to sort such sequences. Will quick-sort going to serve the purpose.

asked Dec 16, 2016 by shivdutt

1 Answer

we will use a minimum heap tree

making a heap tree with n inputs is o(n)

we will assume log(n)=k

but we will use that algorithem to do 2 jobs :

1-making the tree (inserting every element at the sequence).

2-checking if the element that we are trying to insert is already here.

          2-a: we did not found it so we will add it (log(k)) for searchig and log(k) for inserting, overall =2log(k)

          2-b : if it is here we will have 2 keys, the first one for its value, the second key for how much we tried to               insert it, so if it is here we will increase the counter. log(k) for searching.

3-then we will have k size minimum heap, we do delete min every time (log(k)) we will do it k times.

every time we delete we put the result in array according to the counter key, for example if the smallest number is 0 and the counter key is 4 then at our out put we do 4 times 0 : 0000

you will have your sequence sorted.

complexity is : o(nlog(k)+klog(k))

so worest case is order(nlog(log(n))


thank you, I hope any one can found other better algorethem

answered Dec 16, 2016 by Mahdee Kabaha

so extra space will needed for keys O(n).

just o(log(n)) space 1 place for every different element

Yes but i want an algorithm with constant space.