Skip to content

Commit 92e797a

Browse files
author
abuzeid ibrahim
committed
rename variables in the playground
1 parent eb9424d commit 92e797a

File tree

2 files changed

+19
-19
lines changed

2 files changed

+19
-19
lines changed

Insertion Sort/InsertionSort.playground/Contents.swift

Lines changed: 18 additions & 18 deletions
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 ]
Lines changed: 1 addition & 1 deletion
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>

0 commit comments

Comments
 (0)