-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1b814a8
commit 7a66da8
Showing
2 changed files
with
59 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/// Quick Sort Algorithm. | ||
/// Didive and conquer. | ||
/// 1. Pick a pivot point, partition the array. | ||
/// 2. Values to left of pivot are smaller, values to right are larger | ||
/// 3. Repeat steps 1 & 2 and sub-arrays | ||
|
||
public func quickSort(array: inout [Int]) { | ||
quickSort(array: &array, lowIndex: 0, highIndex: array.count - 1) | ||
} | ||
|
||
private func quickSort(array: inout [Int], lowIndex: Int, highIndex: Int) { | ||
if lowIndex < highIndex { | ||
let partitionIndex = partition(array: &array, lowIndex: lowIndex, highIndex: highIndex) | ||
quickSort(array: &array, lowIndex: lowIndex, highIndex: partitionIndex - 1) | ||
quickSort(array: &array, lowIndex: partitionIndex + 1, highIndex: highIndex) | ||
} | ||
} | ||
|
||
private func partition(array: inout [Int], lowIndex: Int, highIndex: Int) -> Int { | ||
// Select last element as pivot value | ||
let pivotValue = array[highIndex] | ||
|
||
// Lowest element index | ||
var lowestIndex = lowIndex | ||
var temp: Int | ||
|
||
// Iterate from low to high, but exclude pivot | ||
for index in lowIndex..<highIndex { | ||
if array[index] <= pivotValue { | ||
// Swap current element with lower element | ||
temp = array[lowestIndex] | ||
array[lowestIndex] = array[index] | ||
array[index] = temp | ||
|
||
// Increment lowest index | ||
lowestIndex += 1 | ||
} | ||
} | ||
|
||
// Move pivot into new position where lowest index left off | ||
temp = array[lowestIndex] | ||
array[lowestIndex] = array[highIndex] | ||
array[highIndex] = temp | ||
|
||
// Return calculated partition (pivot) index | ||
return lowestIndex | ||
} | ||
|
||
var unsortedArray = [8, 6, 4, 100, 67, 1, 98, 7] | ||
var copy = unsortedArray | ||
copy.sort() | ||
quickSort(array: &unsortedArray) | ||
print(unsortedArray) | ||
print(copy) | ||
assert(copy == unsortedArray) |
4 changes: 4 additions & 0 deletions
4
Algorithms/Sorting/QuickSort.playground/contents.xcplayground
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> | ||
<playground version='5.0' target-platform='ios'> | ||
<timeline fileName='timeline.xctimeline'/> | ||
</playground> |