Skip to content

Instantly share code, notes, and snippets.

@jcm93
Last active July 14, 2016 05:20
Show Gist options
  • Save jcm93/4316eca80ff4e28f336376f0ba9ad4d3 to your computer and use it in GitHub Desktop.
Save jcm93/4316eca80ff4e28f336376f0ba9ad4d3 to your computer and use it in GitHub Desktop.
func insert(tracks: [Track], track: Track, isGreater: (a: Track, b: Track) -> Bool?) -> Int {
var high: Int = tracks.count - 1
var low: Int = 0
var index: Int
while (true) {
index = (high - low) / 2
if (high - low) < 2 {
return index
}
print(index)
let result = isGreater(a: track, b: tracks[index])
if result == true {
low = index + 1
}
else if result == false {
high = index - 1
}
else {
return index
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment