Skip to content

Commit 46742c7

Browse files
committed
fixing dumb mistake
1 parent 06e894b commit 46742c7

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

Insertion Sort/InsertionSort.playground/Contents.swift

+15-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,15 @@
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] {
15+
guard array.count > 1 else { return array }
16+
917
var a = array
1018
for x in 1..<a.count {
1119
var y = x
@@ -19,6 +27,10 @@ func insertionSort<T>(_ array: [T], _ isOrderedBefore: (T, T) -> Bool) -> [T] {
1927
return a
2028
}
2129

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
2234
func insertionSort<T: Comparable>(_ array: [T]) -> [T] {
2335
var a = array
2436
for x in 1..<a.count {
@@ -34,6 +46,6 @@ func insertionSort<T: Comparable>(_ array: [T]) -> [T] {
3446
}
3547

3648
let list = [ 10, -1, 3, 9, 2, 27, 8, 5, 1, 3, 0, 26 ]
37-
insertionSort(list)
38-
insertionSort(list, <)
39-
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>

Insertion Sort/InsertionSort.swift

+2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ func insertionSort<T>(_ array: [T], _ isOrderedBefore: (T, T) -> Bool) -> [T] {
2525
/// - Parameter array: the array to be sorted, conatining elements that conform to the Comparable protocol
2626
/// - Returns: a sorted array containing the same elements
2727
func insertionSort<T: Comparable>(_ array: [T]) -> [T] {
28+
guard array.count > 1 else { return array }
29+
2830
var a = array
2931
for x in 1..<a.count {
3032
var y = x

0 commit comments

Comments
 (0)