2
$\begingroup$

I wish to sort $n^2$ numbers which all come from the set $\{1,2,3,...,n\}$, i.e duplications are allowed. I know I can just use merge sort which has complexity $\mathcal{O}(n^2\log (n))$, but I was wondering if it was possible to do better since I know all the numbers will be coming out of $\{1,2,3,...,n\}$.

If there is a special name for this type of problem please let me know. Any references or answers are greatly appreciated.

$\endgroup$
2
  • $\begingroup$ Actually yes. The Ω(n log n) bound only applies to comparison-based sorts. Great question! $\endgroup$ – kakashi10192020 Oct 11 '20 at 3:07
  • $\begingroup$ How many are there in the input of each number from $\{1 \dots n \}$? $\endgroup$ – greybeard Oct 11 '20 at 8:59
4
$\begingroup$

Given your contraints, I think Counting sort or Radix sort will do the job.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.