-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.swift
More file actions
37 lines (33 loc) · 855 Bytes
/
main.swift
File metadata and controls
37 lines (33 loc) · 855 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/*head -n 100 random.txt | swift insertionSort.swift*/
import Foundation
func inptStrings() -> [String] {
var strings : [String] = []
while let x = readLine() {
strings.append(x)
}
return strings
}
func sortInsertion(inptArray: [String], _ debug: Bool) {
var array = inptArray
var pass = 0
var totalSwaps = 0
for count in 0..<array.count {
var swaps = 0
var i = count
while i > 0 {
if array[i] < array[i-1] {
array.swapAt(i-1,i)
swaps += 1
totalSwaps += 1
} else {
break
}
i -= 1
}
if debug {
print("Pass: \(pass), Swaps: \(swaps)/\(totalSwaps), Array: \(array)")
}
pass += 1
}
}
sortInsertion(inptArray: inptStrings(), false)