Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting algorithm problem

Tags:

algorithm

Here's a brain teaser that's been on my mind for a few 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.

Probably something obvious that I am just missing, but I'd appreciate any insight.

like image 216
MichaelM Avatar asked May 18 '26 04:05

MichaelM


1 Answers

Bucket Sort!

Bucket sort, or bin sort, is a sorting algorithm that works by partitioning an array into a number of buckets. Each bucket is then sorted individually, either using a different sorting algorithm, or by recursively applying the bucket sorting algorithm. It is a distribution sort, and is a cousin of radix sort in the most to least significant digit flavour. Bucket sort is a generalization of pigeonhole sort. Since bucket sort is not a comparison sort, the Ω(n log n) lower bound is inapplicable. The computational complexity estimates involve the number of buckets.

like image 150
Bobby Avatar answered May 20 '26 12:05

Bobby



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!