Skip to content

Commit

Permalink
Merge pull request #747 from JulioBBL/insertion-sort
Browse files Browse the repository at this point in the history
Insertion sort improvements
  • Loading branch information
kelvinlauKL committed Oct 3, 2018
2 parents 185571d + 46742c7 commit 4d50254
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 13 deletions.
51 changes: 39 additions & 12 deletions Insertion Sort/InsertionSort.playground/Contents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,47 @@
print("Hello, Swift 4!")
#endif

/// Performs the Insertion sort algorithm to a given array
///
/// - Parameters:
/// - array: the array of elements to be sorted
/// - isOrderedBefore: returns true if the elements provided are in the corect order
/// - Returns: a sorted array containing the same elements
func insertionSort<T>(_ array: [T], _ isOrderedBefore: (T, T) -> Bool) -> [T] {
var a = array
for x in 1..<a.count {
var y = x
let temp = a[y]
while y > 0 && isOrderedBefore(temp, a[y - 1]) {
a[y] = a[y - 1]
y -= 1
guard array.count > 1 else { return array }

var a = array
for x in 1..<a.count {
var y = x
let temp = a[y]
while y > 0 && isOrderedBefore(temp, a[y - 1]) {
a[y] = a[y - 1]
y -= 1
}
a[y] = temp
}
a[y] = temp
}
return a
return a
}

/// Performs the Insertion sort algorithm to a given array
///
/// - Parameter array: the array to be sorted, conatining elements that conform to the Comparable protocol
/// - Returns: a sorted array containing the same elements
func insertionSort<T: Comparable>(_ array: [T]) -> [T] {
var a = array
for x in 1..<a.count {
var y = x
let temp = a[y]
while y > 0 && temp < a[y - 1] {
a[y] = a[y - 1]
y -= 1
}
a[y] = temp
}
return a
}

let list = [ 10, -1, 3, 9, 2, 27, 8, 5, 1, 3, 0, 26 ]
insertionSort(list, <)
insertionSort(list, >)
print(insertionSort(list))
print(insertionSort(list, <))
print(insertionSort(list, >))
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground version='5.0' target-platform='osx'>
<playground version='5.0' target-platform='osx' executeOnSourceChanges='false'>
<timeline fileName='timeline.xctimeline'/>
</playground>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>
26 changes: 26 additions & 0 deletions Insertion Sort/InsertionSort.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/// Performs the Insertion sort algorithm to a given array
///
/// - Parameters:
/// - array: the array of elements to be sorted
/// - isOrderedBefore: returns true if the elements provided are in the corect order
/// - Returns: a sorted array containing the same elements
func insertionSort<T>(_ array: [T], _ isOrderedBefore: (T, T) -> Bool) -> [T] {
guard array.count > 1 else { return array }

Expand All @@ -13,3 +19,23 @@ func insertionSort<T>(_ array: [T], _ isOrderedBefore: (T, T) -> Bool) -> [T] {
}
return a
}

/// Performs the Insertion sort algorithm to a given array
///
/// - Parameter array: the array to be sorted, conatining elements that conform to the Comparable protocol
/// - Returns: a sorted array containing the same elements
func insertionSort<T: Comparable>(_ array: [T]) -> [T] {
guard array.count > 1 else { return array }

var a = array
for x in 1..<a.count {
var y = x
let temp = a[y]
while y > 0 && temp < a[y - 1] {
a[y] = a[y - 1]
y -= 1
}
a[y] = temp
}
return a
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>

0 comments on commit 4d50254

Please sign in to comment.