GeeksforGeeks » Interview Questions
sort an array of 1,0 in log(n) time
(4 posts)-
sort an array of 1,0 in log n time
-
Impossible, the array has to be read at least once => O(n).
But it can be sorted faster than normal O(n log n), the trick is just counting the number of 1s in the first loop O(n) and then rewrite the array in another loop O(n) and the array has been "sorted" in O(n). -
Quick Sort,it can solve it in (n log n).....althogh nt sure 4 algorithm with (log n) time
-
O(lg n) is impossible. However O(n) is possible. A variant of the separation technique used in quicksort can be used. considering pivot to be 0. Anything greater than 0 goes to right and rest to the left. So it will take only one swipe through the array.
:)
Reply
You must log in to post.