For a homework assignment, I am asked to sort an array of bools using a method that uses O(1) space, and O(N) time complexity. Can any hints be offered? I was thinking of something along the pivot method of a quicksort algorithm. -Thanks!
If you have an array of booleans, you can simply count the true (or false) values. Lets assume this results to k. Then you set the first k elements of the array to true and the remaining false.
This algorithm iterates through the array twice (so it has an O(N) time complexity), and only uses one counter, so the space required is O(1).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With