Skip to content

Commit 0087e28

Browse files
authored
Merge pull request kodecocodes#353 from taiheng/feature/swift3-radixsort
Convert RadixSort to swift3 syntax
2 parents e5ce0c7 + 021772e commit 0087e28

File tree

11 files changed

+512
-14
lines changed

11 files changed

+512
-14
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ script:
3333
# - xcodebuild test -project ./Priority\ Queue/Tests/Tests.xcodeproj -scheme Tests
3434
- xcodebuild test -project ./Queue/Tests/Tests.xcodeproj -scheme Tests
3535
# - xcodebuild test -project ./Quicksort/Tests/Tests.xcodeproj -scheme Tests
36+
- xcodebuild test -project ./Radix\ Sort/Tests/Tests.xcodeproj -scheme Tests
3637
- xcodebuild test -project ./Rootish\ Array\ Stack/Tests/Tests.xcodeproj -scheme Tests
3738
# - xcodebuild test -project ./Run-Length\ Encoding/Tests/Tests.xcodeproj -scheme Tests
3839
# - xcodebuild test -project ./Select\ Minimum\ Maximum/Tests/Tests.xcodeproj -scheme Tests
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//: Playground - noun: a place where people can play
2+
3+
import Cocoa
4+
5+
// Test Radix Sort with small array of ten values
6+
var array: [Int] = [19, 4242, 2, 9, 912, 101, 55, 67, 89, 32]
7+
radixSort(&array)
8+
9+
// Test Radix Sort with large array of 1000 random values
10+
var bigArray = [Int](repeating: 0, count: 1000)
11+
var i = 0
12+
while i < 1000 {
13+
bigArray[i] = Int(arc4random_uniform(1000) + 1)
14+
i += 1
15+
}
16+
radixSort(&bigArray)
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
3+
Sorting Algorithm that sorts an input array of integers digit by digit.
4+
5+
*/
6+
import Foundation
7+
8+
// NOTE: This implementation does not handle negative numbers
9+
public func radixSort(_ array: inout [Int] ) {
10+
let radix = 10 //Here we define our radix to be 10
11+
var done = false
12+
var index: Int
13+
var digit = 1 //Which digit are we on?
14+
15+
16+
while !done { //While our sorting is not completed
17+
done = true //Assume it is done for now
18+
19+
var buckets: [[Int]] = [] //Our sorting subroutine is bucket sort, so let us predefine our buckets
20+
21+
for _ in 1...radix {
22+
buckets.append([])
23+
}
24+
25+
26+
for number in array {
27+
index = number / digit //Which bucket will we access?
28+
buckets[index % radix].append(number)
29+
if done && index > 0 { //If we arent done, continue to finish, otherwise we are done
30+
done = false
31+
}
32+
}
33+
34+
var i = 0
35+
36+
for j in 0..<radix {
37+
let bucket = buckets[j]
38+
for number in bucket {
39+
array[i] = number
40+
i += 1
41+
}
42+
}
43+
44+
digit *= radix //Move to the next digit
45+
}
46+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='macos'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>

Radix Sort/RadixSort.playground/playground.xcworkspace/contents.xcworkspacedata

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Radix Sort/Tests/Info.plist

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>CFBundleDevelopmentRegion</key>
6+
<string>en</string>
7+
<key>CFBundleExecutable</key>
8+
<string>$(EXECUTABLE_NAME)</string>
9+
<key>CFBundleIdentifier</key>
10+
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
11+
<key>CFBundleInfoDictionaryVersion</key>
12+
<string>6.0</string>
13+
<key>CFBundleName</key>
14+
<string>$(PRODUCT_NAME)</string>
15+
<key>CFBundlePackageType</key>
16+
<string>BNDL</string>
17+
<key>CFBundleShortVersionString</key>
18+
<string>1.0</string>
19+
<key>CFBundleVersion</key>
20+
<string>1</string>
21+
</dict>
22+
</plist>

Radix Sort/Tests/RadixSortTests.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//
2+
// RadixSortTests.swift
3+
// Tests
4+
//
5+
// Created by theng on 2017-01-10.
6+
// Copyright © 2017 Swift Algorithm Club. All rights reserved.
7+
//
8+
9+
import XCTest
10+
11+
class RadixSortTests: XCTestCase {
12+
func testCombSort() {
13+
let expectedSequence: [Int] = [2, 9, 19, 32, 55, 67, 89, 101, 912, 4242]
14+
var sequence = [19, 4242, 2, 9, 912, 101, 55, 67, 89, 32]
15+
radixSort(&sequence)
16+
XCTAssertEqual(sequence, expectedSequence)
17+
}
18+
}

0 commit comments

Comments
 (0)