Skip to content

Commit

Permalink
Merge pull request kodecocodes#353 from taiheng/feature/swift3-radixsort
Browse files Browse the repository at this point in the history
Convert RadixSort to swift3 syntax
  • Loading branch information
vincentngo authored Jan 15, 2017
2 parents e5ce0c7 + 021772e commit 0087e28
Show file tree
Hide file tree
Showing 11 changed files with 512 additions and 14 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ script:
# - xcodebuild test -project ./Priority\ Queue/Tests/Tests.xcodeproj -scheme Tests
- xcodebuild test -project ./Queue/Tests/Tests.xcodeproj -scheme Tests
# - xcodebuild test -project ./Quicksort/Tests/Tests.xcodeproj -scheme Tests
- xcodebuild test -project ./Radix\ Sort/Tests/Tests.xcodeproj -scheme Tests
- xcodebuild test -project ./Rootish\ Array\ Stack/Tests/Tests.xcodeproj -scheme Tests
# - xcodebuild test -project ./Run-Length\ Encoding/Tests/Tests.xcodeproj -scheme Tests
# - xcodebuild test -project ./Select\ Minimum\ Maximum/Tests/Tests.xcodeproj -scheme Tests
Expand Down
16 changes: 16 additions & 0 deletions Radix Sort/RadixSort.playground/Contents.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//: Playground - noun: a place where people can play

import Cocoa

// Test Radix Sort with small array of ten values
var array: [Int] = [19, 4242, 2, 9, 912, 101, 55, 67, 89, 32]
radixSort(&array)

// Test Radix Sort with large array of 1000 random values
var bigArray = [Int](repeating: 0, count: 1000)
var i = 0
while i < 1000 {
bigArray[i] = Int(arc4random_uniform(1000) + 1)
i += 1
}
radixSort(&bigArray)
46 changes: 46 additions & 0 deletions Radix Sort/RadixSort.playground/Sources/radixSort.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*

Sorting Algorithm that sorts an input array of integers digit by digit.

*/
import Foundation

// NOTE: This implementation does not handle negative numbers
public func radixSort(_ array: inout [Int] ) {
let radix = 10 //Here we define our radix to be 10
var done = false
var index: Int
var digit = 1 //Which digit are we on?


while !done { //While our sorting is not completed
done = true //Assume it is done for now

var buckets: [[Int]] = [] //Our sorting subroutine is bucket sort, so let us predefine our buckets

for _ in 1...radix {
buckets.append([])
}


for number in array {
index = number / digit //Which bucket will we access?
buckets[index % radix].append(number)
if done && index > 0 { //If we arent done, continue to finish, otherwise we are done
done = false
}
}

var i = 0

for j in 0..<radix {
let bucket = buckets[j]
for number in bucket {
array[i] = number
i += 1
}
}

digit *= radix //Move to the next digit
}
}
4 changes: 4 additions & 0 deletions Radix Sort/RadixSort.playground/contents.xcplayground
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground version='5.0' target-platform='macos'>
<timeline fileName='timeline.xctimeline'/>
</playground>

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions Radix Sort/Tests/Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string>
<key>CFBundlePackageType</key>
<string>BNDL</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleVersion</key>
<string>1</string>
</dict>
</plist>
18 changes: 18 additions & 0 deletions Radix Sort/Tests/RadixSortTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//
// RadixSortTests.swift
// Tests
//
// Created by theng on 2017-01-10.
// Copyright © 2017 Swift Algorithm Club. All rights reserved.
//

import XCTest

class RadixSortTests: XCTestCase {
func testCombSort() {
let expectedSequence: [Int] = [2, 9, 19, 32, 55, 67, 89, 101, 912, 4242]
var sequence = [19, 4242, 2, 9, 912, 101, 55, 67, 89, 32]
radixSort(&sequence)
XCTAssertEqual(sequence, expectedSequence)
}
}
Loading

0 comments on commit 0087e28

Please sign in to comment.