-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSorting.swift
More file actions
executable file
·106 lines (93 loc) · 2.96 KB
/
Sorting.swift
File metadata and controls
executable file
·106 lines (93 loc) · 2.96 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
//command
//cat random.txt | head -n 1000 | swift Sorting.swift
import Foundation
var randomWords : [String] = []
while let input = readLine() {
randomWords.append(input)
}
//from apple forums
func flatten(_ array: [Any]) -> [Any] {
var result = [Any]()
for element in array {
if let element = element as? [Any] {
result.append(contentsOf: flatten(element))
} else {
result.append(element)
}
}
return result
}
func priorChar(charA:Character, charB:Character)->Character{
if charA <= charB {
return charA
} else {
return charB
}
}
func returnChar(string:String, offsetBy:Int) -> Character? {
if string.count > offsetBy { //+1
let index = string.index(string.startIndex, offsetBy:offsetBy)
return string[index]
}
return Character(" ")
}
func compareWords(wordA: String, wordB:String) -> String {
if wordA < wordB {
return wordA
} else {
return wordB
}
}
func sortStrings(strings:[String], charCount:Int) -> [Any] {
var Strings: [[Any]] = []
for word in strings {
var added = false
for (count, array) in Strings.enumerated() {
//compare array's first word's first letter with word's first letter
if returnChar(string:array[0] as! String, offsetBy:charCount) == returnChar(string:word, offsetBy:charCount) {
if charCount <= word.count {
Strings[count].append(word)
added = true
}
break
}
}
//if no array with first letter of word was found, make new array with word
if !added {
var position = 0
for array in Strings {
if compareWords(wordA: word,wordB: array[0] as! String) == word {
break
} else {
position += 1
}
}
Strings.insert([word],at:position)
}
}
for (count,array) in Strings.enumerated() {
if array.count > 1 {
Strings[count] = (sortStrings(strings: array as! [String], charCount:charCount + 1))
}
}
return Strings
}
/* lowercased sort */
var randomWordsLower : [String] = []
for word in randomWords {
randomWordsLower.append(word.lowercased())
}
for _ in 0..<5 {
let start = DispatchTime.now()
let sortedStrings = flatten(sortStrings(strings: randomWordsLower, charCount:0))
let end = DispatchTime.now()
let differenceNano = end.uptimeNanoseconds - start.uptimeNanoseconds
let runtime = Double(differenceNano) / 1_000_000.0
print("Runtime: \(runtime) ms")
}
let start2 = DispatchTime.now()
let sortedWords = randomWords.sorted { $0.lowercased() < $1.lowercased() }
let end2 = DispatchTime.now()
let differenceNano2 = end2.uptimeNanoseconds - start2.uptimeNanoseconds
let runtime2 = Double(differenceNano2) / 1_000_000.0
print("Runtime: \(runtime2) ms")