snippetMinor
If I have a large random array of 0s and 1s that I want to sort what kind of an algorithm and data structures should I consider?
Viewed 0 times
randomwantwhatarrayconsideralgorithmkindlargethatshould
Problem
What are the types of things that need to be considered if I need to sort a large random array of 0s and 1s?
You can assume large array is in the order of million or billions.
I understand there are tons of sorting algorithms out there (quick, merge, radix,.etc.) and there are so many different data structures out there (trees, skip lists, linked lists, etc.)
If somebody asks me to sort this large array, do I simply jump to Quick Sort and say that's the best solution? If not, what am I supposed to be thinking about?
I'm not even sure if I know the right answer to this question, but I would really appreciate it if somebody in the community can give some advise.
Thanks.
You can assume large array is in the order of million or billions.
I understand there are tons of sorting algorithms out there (quick, merge, radix,.etc.) and there are so many different data structures out there (trees, skip lists, linked lists, etc.)
If somebody asks me to sort this large array, do I simply jump to Quick Sort and say that's the best solution? If not, what am I supposed to be thinking about?
I'm not even sure if I know the right answer to this question, but I would really appreciate it if somebody in the community can give some advise.
Thanks.
Solution
Use counting sort: run through the array once and count the number of 0's. Then run through the array once more and write in it the counted number of 0's, followed by 1's. In any case, this is a purely academic exercise because nobody would ever need to do such a thing in real life.
Context
StackExchange Computer Science Q#7262, answer score: 9
Revisions (0)
No revisions yet.