This file contains 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 quickSort<T: Comparable>(inout array: [T], var leftIndex: Int = 0, var rightIndex: Int = -1) { | |
// O(n*log(n)) | |
if rightIndex == -1 { //we can call quickSort without start and end index in the beginning so first time we will assign last index of the array to the variable | |
rightIndex = array.count - 1 | |
} | |
var startLeftIndex = leftIndex | |
var startRightIndex = rightIndex | |
var pivot = array[leftIndex] //our pivot is the most left element |
This file contains 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 quickSort(inout array: [Int], var leftIndex: Int = 0, var rightIndex: Int = -1) { | |
// O(n*log(n)) | |
if rightIndex == -1 { //we can call quickSort without start and end index in the beginning so first time we will assign last index of the array to the variable | |
rightIndex = array.count - 1 | |
} | |
var startLeftIndex = leftIndex | |
var startRightIndex = rightIndex | |
var pivot = array[leftIndex] //our pivot is the most left element |