Skip to content

Commit 4d50254

Browse files
authored
Merge pull request #747 from JulioBBL/insertion-sort
Insertion sort improvements
2 parents 185571d + 46742c7 commit 4d50254

File tree

5 files changed

+82
-13
lines changed

5 files changed

+82
-13
lines changed

Insertion Sort/InsertionSort.playground/Contents.swift

+39-12
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,47 @@
55
print("Hello, Swift 4!")
66
#endif
77

8+
/// Performs the Insertion sort algorithm to a given array
9+
///
10+
/// - Parameters:
11+
/// - array: the array of elements to be sorted
12+
/// - isOrderedBefore: returns true if the elements provided are in the corect order
13+
/// - Returns: a sorted array containing the same elements
814
func insertionSort<T>(_ array: [T], _ isOrderedBefore: (T, T) -> Bool) -> [T] {
9-
var a = array
10-
for x in 1..<a.count {
11-
var y = x
12-
let temp = a[y]
13-
while y > 0 && isOrderedBefore(temp, a[y - 1]) {
14-
a[y] = a[y - 1]
15-
y -= 1
15+
guard array.count > 1 else { return array }
16+
17+
var a = array
18+
for x in 1..<a.count {
19+
var y = x
20+
let temp = a[y]
21+
while y > 0 && isOrderedBefore(temp, a[y - 1]) {
22+
a[y] = a[y - 1]
23+
y -= 1
24+
}
25+
a[y] = temp
1626
}
17-
a[y] = temp
18-
}
19-
return a
27+
return a
28+
}
29+
30+
/// Performs the Insertion sort algorithm to a given array
31+
///
32+
/// - Parameter array: the array to be sorted, conatining elements that conform to the Comparable protocol
33+
/// - Returns: a sorted array containing the same elements
34+
func insertionSort<T: Comparable>(_ array: [T]) -> [T] {
35+
var a = array
36+
for x in 1..<a.count {
37+
var y = x
38+
let temp = a[y]
39+
while y > 0 && temp < a[y - 1] {
40+
a[y] = a[y - 1]
41+
y -= 1
42+
}
43+
a[y] = temp
44+
}
45+
return a
2046
}
2147

2248
let list = [ 10, -1, 3, 9, 2, 27, 8, 5, 1, 3, 0, 26 ]
23-
insertionSort(list, <)
24-
insertionSort(list, >)
49+
print(insertionSort(list))
50+
print(insertionSort(list, <))
51+
print(insertionSort(list, >))
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'>
2+
<playground version='5.0' target-platform='osx' executeOnSourceChanges='false'>
33
<timeline fileName='timeline.xctimeline'/>
44
</playground>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>IDEDidComputeMac32BitWarning</key>
6+
<true/>
7+
</dict>
8+
</plist>

Insertion Sort/InsertionSort.swift

+26
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
/// Performs the Insertion sort algorithm to a given array
2+
///
3+
/// - Parameters:
4+
/// - array: the array of elements to be sorted
5+
/// - isOrderedBefore: returns true if the elements provided are in the corect order
6+
/// - Returns: a sorted array containing the same elements
17
func insertionSort<T>(_ array: [T], _ isOrderedBefore: (T, T) -> Bool) -> [T] {
28
guard array.count > 1 else { return array }
39

@@ -13,3 +19,23 @@ func insertionSort<T>(_ array: [T], _ isOrderedBefore: (T, T) -> Bool) -> [T] {
1319
}
1420
return a
1521
}
22+
23+
/// Performs the Insertion sort algorithm to a given array
24+
///
25+
/// - Parameter array: the array to be sorted, conatining elements that conform to the Comparable protocol
26+
/// - Returns: a sorted array containing the same elements
27+
func insertionSort<T: Comparable>(_ array: [T]) -> [T] {
28+
guard array.count > 1 else { return array }
29+
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
39+
}
40+
return a
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>IDEDidComputeMac32BitWarning</key>
6+
<true/>
7+
</dict>
8+
</plist>

0 commit comments

Comments
 (0)