forked from kodecocodes/swift-algorithm-club
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request kodecocodes#353 from taiheng/feature/swift3-radixsort
Convert RadixSort to swift3 syntax
- Loading branch information
Showing
11 changed files
with
512 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
7 changes: 7 additions & 0 deletions
7
Radix Sort/RadixSort.playground/playground.xcworkspace/contents.xcworkspacedata
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
Oops, something went wrong.