Given a collection of 1 million integers, All ranging between 1 to 9, how would you sort them in Big O(n) time ?
This is a typical Integer Sorting problem with a constraint that the number range to sort is very limited in spite 1 million total entries. Integer Sorting with limited range is achieved efficiently with Bucket Sorting.
algorithms…
Integer Sorting Techniques : http://en.wikipedia.org/wiki/Integer_sorting#Algorithms_for_few_items
Sorting Algorithms : http://en.wikipedia.org/wiki/Sorting_algorithm
TIP- What does Wiki Says about Sorting ?
Bucket sort, counting sort, radix sort, and van Emde Boas tree sorting all work best when the key size is small; for large enough keys, they become slower than comparison sortingalgorithms…
Integer Sorting Techniques : http://en.wikipedia.org/wiki/Integer_sorting#Algorithms_for_few_items
Sorting Algorithms : http://en.wikipedia.org/wiki/Sorting_algorithm
Algorithm
Create a array of size 9 and at each index store the occurrence count of the respective integers. Doing this will achieve this sorting with time complexity of Big O(n) and even the memory requirements are minimized. In Order to print the output just traverse the above created array.Read full article from Cracking Java Interviews (Core Java 8, Algorithm, Datastructure, Concurrency, Spring and Hibernate): Given a collection of 1 million integers, All ranging between 1 to 9, how would you sort them in Bi
No comments:
Post a Comment