Skip to content

Instantly share code, notes, and snippets.

@educartoons
Created June 25, 2020 02:26
Show Gist options
  • Select an option

  • Save educartoons/1abf65c6d2f1b0be238591a67b8a01fe to your computer and use it in GitHub Desktop.

Select an option

Save educartoons/1abf65c6d2f1b0be238591a67b8a01fe to your computer and use it in GitHub Desktop.
Implementation of Quick Sort in Swift
// var numbers: [Int] = [15, 67, 8, 16, 44, 27, 12, 35];
// We will call quicksort with
// quickSort(&numbers, p: 0, r: numbers.count-1);
func quickSort(_ numbers: inout [Int], p: Int, r: Int) -> Void{
if p<r {
let q = partition(&numbers, p: p, r: r)
quickSort(&numbers, p: p, r: q-1);
quickSort(&numbers, p: q+1, r: r)
}
}
func partition(_ numbers: inout [Int],p: Int, r:Int) -> Int{
let x = numbers[r]
var i = p - 1;
for j in p..<r{
if numbers[j] <= x {
i = i + 1;
numbers.swapAt(i, j)
}
}
numbers.swapAt(i+1, r)
return i+1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment