Created
April 27, 2021 15:12
-
-
Save thatswiftguy/1b1db6f07da80c639216812541fc74a4 to your computer and use it in GitHub Desktop.
Quick Sort Algorithm
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
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