Skip to content

Instantly share code, notes, and snippets.

@nderkach
Created September 28, 2019 16:35
Show Gist options
  • Save nderkach/c1c6cd2a216b501e3e40c9e8465bc1b6 to your computer and use it in GitHub Desktop.
Save nderkach/c1c6cd2a216b501e3e40c9e8465bc1b6 to your computer and use it in GitHub Desktop.
Sort a Bit Array
# 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