Skip to content

Commit e93f3c6

Browse files
authored
Merge pull request kodecocodes#887 from abuzeid-ibrahim/master
Insersion sort: rename variables with convenience names
2 parents 82494bf + b47e620 commit e93f3c6

File tree

4 files changed

+56
-56
lines changed

4 files changed

+56
-56
lines changed

Insertion Sort/InsertionSort.playground/Contents.swift

+18-18
Original file line numberDiff line numberDiff line change
@@ -9,35 +9,35 @@
99
func insertionSort<T>(_ array: [T], _ isOrderedBefore: (T, T) -> Bool) -> [T] {
1010
guard array.count > 1 else { return array }
1111

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
12+
var sortedArray = array
13+
for index in 1..<sortedArray.count {
14+
var currentIndex = index
15+
let temp = sortedArray[currentIndex]
16+
while currentIndex > 0 && isOrderedBefore(temp, sortedArray[currentIndex - 1]) {
17+
sortedArray[currentIndex] = sortedArray[currentIndex - 1]
18+
currentIndex -= 1
1919
}
20-
a[y] = temp
20+
sortedArray[currentIndex] = temp
2121
}
22-
return a
22+
return sortedArray
2323
}
2424

2525
/// Performs the Insertion sort algorithm to a given array
2626
///
2727
/// - Parameter array: the array to be sorted, conatining elements that conform to the Comparable protocol
2828
/// - Returns: a sorted array containing the same elements
2929
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
30+
var sortedArray = array
31+
for index in 1..<sortedArray.count {
32+
var currentIndex = index
33+
let temp = sortedArray[currentIndex]
34+
while currentIndex > 0 && temp < sortedArray[currentIndex - 1] {
35+
sortedArray[currentIndex] = sortedArray[currentIndex - 1]
36+
currentIndex -= 1
3737
}
38-
a[y] = temp
38+
sortedArray[currentIndex] = temp
3939
}
40-
return a
40+
return sortedArray
4141
}
4242

4343
let list = [ 10, -1, 3, 9, 2, 27, 8, 5, 1, 3, 0, 26 ]
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2-
<playground version='5.0' target-platform='osx' executeOnSourceChanges='false'>
2+
<playground version='5.0' target-platform='osx'>
33
<timeline fileName='timeline.xctimeline'/>
44
</playground>

Insertion Sort/InsertionSort.swift

+21-21
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,19 @@
55
/// - isOrderedBefore: returns true if the elements provided are in the corect order
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+
/// - sortedArray: copy the array to save stability
10+
var sortedArray = array
11+
for index in 1..<sortedArray.count {
12+
var currentIndex = index
13+
let temp = sortedArray[currentIndex]
14+
while currentIndex > 0, isOrderedBefore(temp, sortedArray[currentIndex - 1]) {
15+
sortedArray[currentIndex] = sortedArray[currentIndex - 1]
16+
currentIndex -= 1
17+
}
18+
sortedArray[currentIndex] = temp
1719
}
18-
a[y] = temp
19-
}
20-
return a
20+
return sortedArray
2121
}
2222

2323
/// Performs the Insertion sort algorithm to a given array
@@ -27,15 +27,15 @@ func insertionSort<T>(_ array: [T], _ isOrderedBefore: (T, T) -> Bool) -> [T] {
2727
func insertionSort<T: Comparable>(_ array: [T]) -> [T] {
2828
guard array.count > 1 else { return array }
2929

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
30+
var sortedArray = array
31+
for index in 1..<sortedArray.count {
32+
var currentIndex = index
33+
let temp = sortedArray[currentIndex]
34+
while currentIndex > 0, temp < sortedArray[currentIndex - 1] {
35+
sortedArray[currentIndex] = sortedArray[currentIndex - 1]
36+
currentIndex -= 1
3737
}
38-
a[y] = temp
38+
sortedArray[currentIndex] = temp
3939
}
40-
return a
40+
return sortedArray
4141
}

Insertion Sort/README.markdown

+16-16
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,15 @@ Here is an implementation of insertion sort in Swift:
9191

9292
```swift
9393
func insertionSort(_ array: [Int]) -> [Int] {
94-
var a = array // 1
95-
for x in 1..<a.count { // 2
96-
var y = x
97-
while y > 0 && a[y] < a[y - 1] { // 3
98-
a.swapAt(y - 1, y)
99-
y -= 1
94+
var sortedArray = array // 1
95+
for index in 1..<sortedArray.count { // 2
96+
var currentIndex = index
97+
while currentIndex > 0 && sortedArray[currentIndex] < sortedArray[currentIndex - 1] { // 3
98+
sortedArray.swapAt(currentIndex - 1, currentIndex)
99+
currentIndex -= 1
100100
}
101101
}
102-
return a
102+
return sortedArray
103103
}
104104

105105

@@ -154,17 +154,17 @@ In code that looks like this:
154154

155155
```swift
156156
func insertionSort(_ array: [Int]) -> [Int] {
157-
var a = array
158-
for x in 1..<a.count {
159-
var y = x
160-
let temp = a[y]
161-
while y > 0 && temp < a[y - 1] {
162-
a[y] = a[y - 1] // 1
163-
y -= 1
157+
var sortedArray = array
158+
for index in 1..<sortedArray.count {
159+
var currentIndex = index
160+
let temp = sortedArray[currentIndex]
161+
while currentIndex > 0 && temp < sortedArray[currentIndex - 1] {
162+
sortedArray[currentIndex] = sortedArray[currentIndex - 1] // 1
163+
currentIndex -= 1
164164
}
165-
a[y] = temp // 2
165+
sortedArray[currentIndex] = temp // 2
166166
}
167-
return a
167+
return sortedArray
168168
}
169169
```
170170

0 commit comments

Comments
 (0)