The task of the sorting algorithm

Here's a brain teaser that has been in my opinion for several days.

We have a sequence S of n elements. Each element is an integer in the range [0, n ^ 2-1]. Describe a simple method for sorting S in O (n) time.

Maybe something obvious that I'm just missing out, but I would appreciate any insight.

+3
source share
4 answers

Bucket Sorting!

bin - , . , , . , radix . - . , Ω (n log n) . .

+4

n , ( n).

O (n) , O (n) .

+2
+2

Quicksort is O (n log n) as the standard “good algo” method for sorting a list. So there must be some kind of “trick” to go to O (n) time.

The only real trick for this data is from 0 to n ^ 2-1 ... but I can’t imagine how it can be used to sort in O (n) time ....

PS Sounds like homework, not that you are a "puzzle for knowledge"

PPS I can't help but think of sorting the bucket.

0
source

Source: https://habr.com/ru/post/1768927/


All Articles