Sorting
suggest changeBubble Sort
This is a simple sorting algorithm that repeatedly steps through the list to be sorted, compares each pair of adjacent items and swaps them if they are in the wrong order. The pass through the list is repeated until no swaps are needed. Although the algorithm is simple, it is too slow and impractical for most problems. It is O(n²) and slower than insertion sort for most inputs.
extension Array where Element: Comparable {
func bubbleSort() -> Array<Element> {
//check for trivial case
guard self.count > 1 else {
return self
}
//mutated copy
var output: Array<Element> = self
for primaryIndex in 0..<self.count {
let passes = (output.count - 1) - primaryIndex
//"half-open" range operator
for secondaryIndex in 0..<passes {
let key = output[secondaryIndex]
//compare / swap positions
if (key > output[secondaryIndex + 1]) {
output.swapAt(secondaryIndex, secondaryIndex + 1)
}
}
}
return output
}
}
Insertion sort
Insertion sort walks the collection and places each element by its value. A typical insertion sort is O(n²). The method below uses remove and insert inside the inner loop, so it does more work than a swap-based insertion sort.
extension Array where Element: Comparable {
func insertionSort() -> Array<Element> {
//check for trivial case
guard self.count > 1 else {
return self
}
//mutated copy
var output: Array<Element> = self
for primaryindex in 0..<output.count {
let key = output[primaryindex]
var secondaryindex = primaryindex
while secondaryindex > -1 {
if key < output[secondaryindex] {
//move to correct position
output.remove(at: secondaryindex + 1)
output.insert(key, at: secondaryindex)
}
secondaryindex -= 1
}
}
return output
}
}
Selection sort
Selection sort takes the first unsorted index as the minimum, walks the rest of the array, and keeps any smaller value it finds. It then swaps that value into place and repeats. It is O(n²), and on nearly sorted data it is often slower than insertion sort.
extension Array where Element: Comparable {
func selectionSort() -> Array<Element> {
//check for trivial case
guard self.count > 1 else {
return self
}
//mutated copy
var output: Array<Element> = self
for primaryindex in 0..<output.count {
var minimum = primaryindex
var secondaryindex = primaryindex + 1
while secondaryindex < output.count {
//store lowest value as minimum
if output[minimum] > output[secondaryindex] {
minimum = secondaryindex
}
secondaryindex += 1
}
//swap minimum value with array iteration
if primaryindex != minimum {
output.swapAt(primaryindex, minimum)
}
}
return output
}
}
Quick Sort - O(n log n) average time
Quicksort is a divide-and-conquer sort. Its average time is O(n log n). The worst case for this pivot choice is O(n²). It partitions the array into values below the pivot and values above it, then sorts those parts.
The steps are:
Pick an element, called a pivot, from the array.
Reorder the array so that all elements with values less than the pivot come before the pivot, while all elements with values greater than the pivot come after it (equal values can go either way). After this partitioning, the pivot is in its final position. This is called the partition operation.
Recursively apply the above steps to the sub-array of elements with smaller values and separately to the sub-array of elements with greater values.
extension Array where Element: Comparable {
mutating func quickSort() -> Array<Element> {
func qSort(start startIndex: Int, _ pivot: Int) {
if (startIndex < pivot) {
let iPivot = qPartition(start: startIndex, pivot)
qSort(start: startIndex, iPivot - 1)
qSort(start: iPivot + 1, pivot)
}
}
qSort(start: 0, self.endIndex - 1)
return self
}
mutating func qPartition(start startIndex: Int, _ pivot: Int) -> Int {
var wallIndex: Int = startIndex
//compare range with pivot
for currentIndex in wallIndex..<pivot {
if self[currentIndex] <= self[pivot] {
if wallIndex != currentIndex {
self.swapAt(currentIndex, wallIndex)
}
//advance wall
wallIndex += 1
}
}
//move pivot to final position
if wallIndex != pivot {
self.swapAt(wallIndex, pivot)
}
return wallIndex
}
}