Skip to content

Commit d0aa7dd

Browse files
committed
insertion sort: performance improvements
1 parent 667d265 commit d0aa7dd

File tree

2 files changed

+44
-50
lines changed

2 files changed

+44
-50
lines changed

Insertion Sort/InsertionSort.playground/Contents.swift

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,40 @@
1-
//: Playground - noun: a place where people can play
2-
31
/// Performs the Insertion sort algorithm to a given array
42
///
53
/// - Parameters:
64
/// - array: the array of elements to be sorted
7-
/// - isOrderedBefore: returns true if the elements provided are in the corect order
5+
/// - isOrderedBefore: function that given to inputs returns whether should be order before or after. Allows for different sorts (e.g. change in direction).
86
/// - Returns: a sorted array containing the same elements
97
func insertionSort<T>(_ array: [T], _ isOrderedBefore: (T, T) -> Bool) -> [T] {
108
guard array.count > 1 else { return array }
11-
12-
var a = array
13-
for x in 1..<a.count {
14-
var y = x
15-
let temp = a[y]
16-
while y > 0 && isOrderedBefore(temp, a[y - 1]) {
17-
a[y] = a[y - 1]
18-
y -= 1
9+
var sortedArray = array
10+
for index in 1..<sortedArray.count {
11+
if isOrderedBefore(sortedArray[index-1], sortedArray[index]) { continue }
12+
for currentIndex in (0..<index).reversed() {
13+
if isOrderedBefore(sortedArray[currentIndex], sortedArray[index]) {
14+
sortedArray.move(from: index, to: currentIndex + 1)
15+
break
16+
} else if currentIndex == 0 {
17+
sortedArray.move(from: index, to: 0)
18+
}
1919
}
20-
a[y] = temp
2120
}
22-
return a
21+
return sortedArray
2322
}
2423

2524
/// Performs the Insertion sort algorithm to a given array
2625
///
27-
/// - Parameter array: the array to be sorted, conatining elements that conform to the Comparable protocol
28-
/// - Returns: a sorted array containing the same elements
26+
/// - Parameter array: the array to be sorted, containing elements that conform to the Comparable protocol
27+
/// - Returns: a sorted array containing the same elements ordered from lowest to highest
2928
func insertionSort<T: Comparable>(_ array: [T]) -> [T] {
30-
var a = array
31-
for x in 1..<a.count {
32-
var y = x
33-
let temp = a[y]
34-
while y > 0 && temp < a[y - 1] {
35-
a[y] = a[y - 1]
36-
y -= 1
37-
}
38-
a[y] = temp
29+
return insertionSort(array, <)
30+
}
31+
32+
fileprivate extension Array {
33+
mutating func move(from: Int, to: Int) {
34+
let removed = self[from]
35+
self.remove(at: from)
36+
self.insert(removed, at: to)
3937
}
40-
return a
4138
}
4239

4340
let list = [ 10, -1, 3, 9, 2, 27, 8, 5, 1, 3, 0, 26 ]

Insertion Sort/InsertionSort.swift

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,37 @@
22
///
33
/// - Parameters:
44
/// - array: the array of elements to be sorted
5-
/// - isOrderedBefore: returns true if the elements provided are in the corect order
5+
/// - isOrderedBefore: function that given to inputs returns whether should be order before or after. Allows for different sorts (e.g. change in direction).
66
/// - Returns: a sorted array containing the same elements
77
func insertionSort<T>(_ array: [T], _ isOrderedBefore: (T, T) -> Bool) -> [T] {
8-
guard array.count > 1 else { return array }
9-
10-
var a = array
11-
for x in 1..<a.count {
12-
var y = x
13-
let temp = a[y]
14-
while y > 0 && isOrderedBefore(temp, a[y - 1]) {
15-
a[y] = a[y - 1]
16-
y -= 1
8+
guard array.count > 1 else { return array }
9+
var sortedArray = array
10+
for index in 1..<sortedArray.count {
11+
if isOrderedBefore(sortedArray[index-1], sortedArray[index]) { continue }
12+
for currentIndex in (0..<index).reversed() {
13+
if isOrderedBefore(sortedArray[currentIndex], sortedArray[index]) {
14+
sortedArray.move(from: index, to: currentIndex + 1)
15+
break
16+
} else if currentIndex == 0 {
17+
sortedArray.move(from: index, to: 0)
18+
}
19+
}
1720
}
18-
a[y] = temp
19-
}
20-
return a
21+
return sortedArray
2122
}
2223

2324
/// Performs the Insertion sort algorithm to a given array
2425
///
2526
/// - Parameter array: the array to be sorted, containing elements that conform to the Comparable protocol
26-
/// - Returns: a sorted array containing the same elements
27+
/// - Returns: a sorted array containing the same elements ordered from lowest to highest
2728
func insertionSort<T: Comparable>(_ array: [T]) -> [T] {
28-
guard array.count > 1 else { return array }
29+
return insertionSort(array, <)
30+
}
2931

30-
var a = array
31-
for x in 1..<a.count {
32-
var y = x
33-
let temp = a[y]
34-
while y > 0 && temp < a[y - 1] {
35-
a[y] = a[y - 1]
36-
y -= 1
37-
}
38-
a[y] = temp
32+
fileprivate extension Array {
33+
mutating func move(from: Int, to: Int) {
34+
let removed = self[from]
35+
self.remove(at: from)
36+
self.insert(removed, at: to)
3937
}
40-
return a
4138
}

0 commit comments

Comments
 (0)