File tree Expand file tree Collapse file tree 1 file changed +13
-14
lines changed Expand file tree Collapse file tree 1 file changed +13
-14
lines changed Original file line number Diff line number Diff line change 5
5
*/
6
6
7
7
8
- func radixSort( inout arr: [ Int ] ) {
9
-
10
-
8
+ // NOTE: This implementation does not handle negative numbers
9
+ func radixSort( _ array: inout [ Int ] ) {
11
10
let radix = 10 //Here we define our radix to be 10
12
11
var done = false
13
12
var index : Int
14
13
var digit = 1 //Which digit are we on?
15
-
16
-
14
+
15
+
17
16
while !done { //While our sorting is not completed
18
17
done = true //Assume it is done for now
19
-
18
+
20
19
var buckets : [ [ Int ] ] = [ ] //Our sorting subroutine is bucket sort, so let us predefine our buckets
21
-
20
+
22
21
for _ in 1 ... radix {
23
22
buckets. append ( [ ] )
24
23
}
25
-
26
-
27
- for number in arr {
24
+
25
+
26
+ for number in array {
28
27
index = number / digit //Which bucket will we access?
29
28
buckets [ index % radix] . append ( number)
30
29
if done && index > 0 { //If we arent done, continue to finish, otherwise we are done
31
30
done = false
32
31
}
33
32
}
34
-
33
+
35
34
var i = 0
36
-
35
+
37
36
for j in 0 ..< radix {
38
37
let bucket = buckets [ j]
39
38
for number in bucket {
40
- arr [ i] = number
39
+ array [ i] = number
41
40
i += 1
42
41
}
43
42
}
44
-
43
+
45
44
digit *= radix //Move to the next digit
46
45
}
47
46
}
You can’t perform that action at this time.
0 commit comments