Last active
December 3, 2016 15:22
-
-
Save pofat/88ac7582cf4e8d4a5c5b00dbf3026faa to your computer and use it in GitHub Desktop.
Implement algorithms with Swift 3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This implementation is originally from `https://boxueio.com` | |
typealias Criterial<T> = (T, T) -> Bool | |
func insertionSortOf<T: Comparable>(_ coll: [T], byCriteria: Criterial<T> = { $0 < $1 }) -> [T] { | |
guard coll.count > 1 else { | |
return coll | |
} | |
var result = coll | |
for x in 1 ..< coll.count { | |
var y = x | |
let key = result[y] | |
print("Get \(key)") | |
while y > 0 && byCriteria(key, result[y - 1]) { | |
print("------------") | |
print("Remove \(key) at position \(y)") | |
print("Insert \(key) at position \(y - 1)") | |
print("------------") | |
result.remove(at: y) | |
result.insert(key, at: y - 1) | |
y -= 1 | |
} | |
} | |
return result | |
} | |
let a = [1, 5, 6] | |
print(insertionSortOf(a)) | |
print(insertionSortOf(a, byCriteria: >)) | |
// Another insertion algorithm | |
print("--------- Another implementation for insertion sort ---------") | |
func newInsertionSortOf<T: Comparable>(_ coll: [T], byCriteria: Criterial<T> = { $0 < $1 }) -> [T] { | |
guard coll.count > 1 else { | |
return coll | |
} | |
var result = coll | |
for x in 1 ..< coll.count { | |
var y = x | |
let key = result[y] | |
print("Get \(key)") | |
while y > 0 && byCriteria(key, result[y - 1]) { | |
print("----------") | |
print("Shift \(result[y - 1]) right for one digit ") | |
result[y] = result[y - 1] | |
y -= 1 | |
} | |
print("Now insert \(key) at position : \(y)") | |
result[y] = key | |
} | |
return result | |
} | |
let b = [3, 6, 8] | |
print("***** Ascending *******") | |
print(newInsertionSortOf(b)) | |
print(" ****** Descending ********") | |
print(newInsertionSortOf(b, byCriteria: >)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* This implementation is originally from `https://boxueio.com` | |
* | |
* Sort [1, 5, 7, 6] in descedning | |
* | |
*/ | |
typealias Criteria<T> = (T, T) -> Bool | |
func selectionSortOf<T: Comparable>(_ coll: [T], byCriteria: Criteria<T> = { $0 < $1 }) -> [T] { | |
guard coll.count > 1 else { | |
return coll | |
} | |
// record how many shift | |
var times = 0 | |
var result = coll | |
for x in 0 ..< coll.count - 1 { | |
var candidateIndex = x | |
print("-------------------") | |
print("Sorted:\t\(result[0 ..< candidateIndex])") | |
print("Unsorted:\t\(result[candidateIndex ..< result.count])") | |
for y in x + 1 ..< coll.count { | |
times += 1 | |
if byCriteria(result[candidateIndex], result[y]) == false { | |
print("compare \(result[candidateIndex]) and \(result[y]) failed to pass criteria" ) | |
candidateIndex = y | |
break | |
} | |
} | |
if (candidateIndex != x) { | |
print("Swap \(result[candidateIndex]) and \(result[x])") | |
swap(&result[candidateIndex], &result[x]) | |
} | |
print("Current unsorted array after swap: \(result[x ..< result.count])") | |
print(">>> Move \(result[x]) into sorted portion") | |
} | |
print("Take \(times) times to sort this array") | |
return result | |
} | |
let a = [1, 5, 7, 6] | |
print("----- Ascending ---------") | |
print(selectionSortOf(a)) | |
print("----- Descedning ----------") | |
print(selectionSortOf(a, byCriteria: >)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment