Skip to content

Instantly share code, notes, and snippets.

@outrowender
Last active December 13, 2023 22:47
Show Gist options
  • Save outrowender/1a86ab7400f3662380a4f399bcee0903 to your computer and use it in GitHub Desktop.
Save outrowender/1a86ab7400f3662380a4f399bcee0903 to your computer and use it in GitHub Desktop.
Swift QuickSort example
func qs(_ list: [Int]) -> [Int] {
if list.count < 1 { return list }
let pivot = list[list.count-1]
var l = [Int]()
var e = [Int]()
var r = [Int]()
for x in list {
if x < pivot { l.append(x) }
if x == pivot { e.append(x) }
if x > pivot { r.append(x) }
}
return qs(l) + e + qs(r)
}
@outrowender
Copy link
Author

It lacks some optimization, but the ideia is there

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment