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.
Asked By : user1068636
Answered By : Andrej Bauer
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.
Best Answer from StackOverflow
Question Source : http://cs.stackexchange.com/questions/7262
0 comments:
Post a Comment
Let us know your responses and feedback