GeeksforGeeks » Interview Questions

sort an array of 1,0 in log(n) time

(4 posts)
  • Started 7 months ago by sangee
  • Latest reply from angel_eyez
  1. sangee
    guest
    Posted 7 months ago #

    sort an array of 1,0 in log n time

  2. rurouni
    guest
    Posted 7 months ago #

    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).

  3. kamlesh meghwal
    guest
    Posted 7 months ago #

    Quick Sort,it can solve it in (n log n).....althogh nt sure 4 algorithm with (log n) time

  4. angel_eyez
    Member
    Posted 6 months ago #

    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.

RSS feed for this topic