Skip to content

Commit 2bc9a0c

Browse files
author
Chris Pilcher
committed
Merge pull request kodecocodes#102 from axptwig/master
Claim several data structures and algorithms for a group project
2 parents 6f0d3d9 + 610b532 commit 2bc9a0c

21 files changed

+2257
-0
lines changed

Radix Sort/ReadMe.md

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Radix Sort
2+
3+
Radix sort is a sorting algorithm that takes as input an array of integers and uses a sorting subroutine( that is often another efficient sorting algorith) to sort the integers by their radix, or rather their digit. Counting Sort, and Bucket Sort are often times used as the subroutine for Radix Sort.
4+
5+
##Example
6+
7+
* Input Array: [170, 45, 75, 90, 802, 24, 2, 66]
8+
* Output Array (Sorted): [2, 24, 45, 66, 75, 90, 170, 802]
9+
10+
###Step 1:
11+
The first step in this algorithm is to define the digit or rather the "base" or radix that we will use to sort.
12+
For this example we will let radix = 10, since the integers we are working with in the example are of base 10.
13+
14+
###Step 2:
15+
The next step is to simply iterate n times (where n is the number of digits in the largest integer in the input array), and upon each iteration perform a sorting subroutine on the current digit in question.
16+
17+
###Algorithm in Action
18+
19+
Let's take a look at our example input array.
20+
21+
The largest integer in our array is 802, and it has three digits (ones, tens, hundreds). So our algorithm will iterate three times whilst performing some sorting algorithm on the digits of each integer.
22+
23+
* Iteration 1: 170, 90, 802, 2, 24, 45, 75, 66
24+
* Iteration 2: 802, 2, 24, 45, 66, 170, 75, 90
25+
* Iteration 3: 2, 24, 45, 66, 75, 90, 170, 802
26+
27+
28+
29+
See also [Wikipedia](https://en.wikipedia.org/wiki/Radix_Sort).
30+
31+
*Written for the Swift Algorithm Club by Christian Encarnacion*

Radix Sort/radixSort.swift

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
3+
Sorting Algorithm that sorts an input array of integers digit by digit.
4+
5+
*/
6+
7+
8+
func radixSort(inout arr: [Int] ) {
9+
10+
11+
let radix = 10 //Here we define our radix to be 10
12+
var done = false
13+
var index: Int
14+
var digit = 1 //Which digit are we on?
15+
16+
17+
while !done { //While our sorting is not completed
18+
done = true //Assume it is done for now
19+
20+
var buckets: [[Int]] = [] //Our sorting subroutine is bucket sort, so let us predefine our buckets
21+
22+
for _ in 1...radix {
23+
buckets.append([])
24+
}
25+
26+
27+
for number in arr {
28+
index = number / digit //Which bucket will we access?
29+
buckets[index % radix].append(number)
30+
if done && index > 0 { //If we arent done, continue to finish, otherwise we are done
31+
done = false
32+
}
33+
}
34+
35+
var i = 0
36+
37+
for j in 0..<radix {
38+
let bucket = buckets[j]
39+
for number in bucket {
40+
arr[i] = number
41+
i += 1
42+
}
43+
}
44+
45+
digit *= radix //Move to the next digit
46+
}
47+
}

0 commit comments

Comments
 (0)