Skip to content

Instantly share code, notes, and snippets.

@thatswiftguy
Created April 27, 2021 15:12
Show Gist options
  • Save thatswiftguy/1b1db6f07da80c639216812541fc74a4 to your computer and use it in GitHub Desktop.
Save thatswiftguy/1b1db6f07da80c639216812541fc74a4 to your computer and use it in GitHub Desktop.
Quick Sort Algorithm
var myarray4 = [4,6,2,7,7,3,5]
func quickSort(array: inout [Int], startIndex: Int, endIndex: Int) {
if startIndex >= endIndex {
return
}
let pivot = partition(array: &array, startIndex: startIndex, endIndex: endIndex)
quickSort(array: &array, startIndex: startIndex, endIndex: pivot-1)
quickSort(array: &array, startIndex: pivot+1, endIndex: endIndex)
}
func partition(array: inout [Int], startIndex: Int, endIndex: Int) -> Int {
var q = startIndex
for index in startIndex..<endIndex {
if array[index] < array[endIndex] {
array.swapAt(q, index)
q += 1
}
}
array.swapAt(q, endIndex)
return q
}
quickSort(array: &myarray4, startIndex: 0, endIndex: 6)
print(myarray4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment