Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the fastest way to sort 1 million integers when integers are from the range [1,100]?

Tags:

Notes: I've thought about Radix sort, bucket sort, counting sort.

Is there anyway to achieve big O(n)?

like image 883
wiznaibus Avatar asked Jul 22 '10 16:07

wiznaibus


People also ask

What is the best way to sort 1 million integers?

You can use counting sort. Counting sort (sometimes referred to as ultra sort or math sort) is a sorting algorithm which (like bucket sort) takes advantage of knowing the range of the numbers in the array to be sorted (array A).

What's the fastest way to sort 1 million 32 bit integers?

If it's meant as speed, the radix sort is the fastest theoretical sort for this. Especially as we know that the integers are 32bit and thus can only be in a range of values.

Which is the fastest method of sorting?

But since it has the upper hand in the average cases for most inputs, Quicksort is generally considered the “fastest” sorting algorithm.

What is the best sorting algorithm to use for more than 1 million elements in an array in general?

What is the best sorting algorithm to use for the elements in array are more than 1 million in general? Merge sort.


2 Answers

You can use counting sort.

Counting sort (sometimes referred to as ultra sort or math sort) is a sorting algorithm which (like bucket sort) takes advantage of knowing the range of the numbers in the array to be sorted (array A).

Counting sort is a stable sort and has a running time of Θ(n+k), where n and k are the lengths of the arrays A (the input array) and C (the counting array), respectively. In order for this algorithm to be efficient, k must not be much larger than n.

In this case k is 100 and n is 1000000.

like image 52
Mark Byers Avatar answered Sep 20 '22 15:09

Mark Byers


A counting sort would be the obvious choice under these circumstances. Yes, properly implemented it should have linear complexity.

like image 32
Jerry Coffin Avatar answered Sep 20 '22 15:09

Jerry Coffin