Last active
December 13, 2023 22:47
-
-
Save outrowender/1a86ab7400f3662380a4f399bcee0903 to your computer and use it in GitHub Desktop.
Swift QuickSort example
This file contains hidden or 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
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) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It lacks some optimization, but the ideia is there