Created
September 28, 2019 16:35
-
-
Save nderkach/c1c6cd2a216b501e3e40c9e8465bc1b6 to your computer and use it in GitHub Desktop.
Sort a Bit Array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Sort a Bit Array | |
func bitSort(_ arr: inout [Int]) { | |
var zerosCount = arr.filter { $0 == 0 }.count | |
var idx = 0 | |
while idx < arr.count { | |
arr[idx] = zerosCount > 0 ? 0 : 1 | |
zerosCount -= 1 | |
idx += 1 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment