Skip to content

Commit e7f578c

Browse files
committed
Radix Sort code and README file added
1 parent 90b5212 commit e7f578c

File tree

6 files changed

+155
-3
lines changed

6 files changed

+155
-3
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
- Bucket Sort
3737
- [Counting Sort](./Sorting/Counting%20Sort/)
3838
- Heap Sort
39-
- Radix Sort
39+
- [Radix Sort](./Sorting/Radix%20Sort/)
4040

4141

4242
- Graph Algorithms

Sorting/Counting Sort/counting_sort.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ function countingSort(arr) {
2525
return sortedArray;
2626
}
2727

28-
/******************* Testing Selection sort algorithm *********************/
28+
/******************* Testing Counting sort algorithm *********************/
2929

3030
/**
3131
* Returns a random integer between min (inclusive) and max (inclusive)

Sorting/Insertion Sort/insertion-sort.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ function insertionSort(arr) {
1919
return arr;
2020
}
2121

22-
/******************* Testing Selection sort algorithm *********************/
22+
/******************* Testing Insertion sort algorithm *********************/
2323

2424
/**
2525
* Returns a random integer between min (inclusive) and max (inclusive)

Sorting/Radix Sort/README.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# Radix Sort
2+
3+
**Radix sort** is a non-comparative integer sorting algorithm that sorts data with integer keys by grouping keys by the individual digits which share the same significant position and value. The idea of Radix Sort is to do digit by digit sort starting from least significant digit to most significant digit. Radix sort uses [counting sort](../Counting%20Sort/) as a subroutine to sort.
4+
5+
#### Idea:
6+
![Radix Sort](./images/radix-sort.png)
7+
8+
Assume the input array is:
9+
123, 2, 999, 609, 111
10+
11+
Based on the algorithm, we will sort the input array according to the one's digit (least significant digit).
12+
13+
```
14+
[0]:
15+
[1]: 111
16+
[2]: 2
17+
[3]: 123
18+
[4]:
19+
[5]:
20+
[6]:
21+
[7]:
22+
[8]:
23+
[9]: 999, 609
24+
```
25+
26+
So, the array becomes 111, 2, 123, 999, 609. Note that we placed 999 before 609 because it appeared first.
27+
28+
Now, we'll sort according to the ten's digit:
29+
30+
```
31+
[0]: 609, 2
32+
[1]: 111
33+
[2]: 123
34+
[3]:
35+
[4]:
36+
[5]:
37+
[6]:
38+
[7]:
39+
[8]:
40+
[9]: 999
41+
```
42+
43+
Now, the array becomes : 609, 2, 111, 123, 999
44+
45+
Finally , we sort according to the hundred's digit (most significant digit):
46+
47+
```
48+
[0]: 2
49+
[1]: 111, 123
50+
[2]:
51+
[3]:
52+
[4]:
53+
[5]:
54+
[6]: 609
55+
[7]:
56+
[8]:
57+
[9]: 999
58+
```
59+
60+
The array becomes : 2, 111, 123, 609, 999 which is sorted. This is how Radix Sort algorithm works.
61+
62+
63+
#### Video Tutorial
64+
<a href="http://www.youtube.com/watch?feature=player_embedded&v=YXFI4osELGU" target="_blank"><img src="http://img.youtube.com/vi/YXFI4osELGU/0.jpg"
65+
alt="Counting Sort (youtube)" width="640" height="480" border="10" /></a>
66+
67+
68+
#### Complexity Analysis
69+
The time complexity for radix sort is : O(d(n+b)), where b is the base for representing numbers, for example, for decimal system, b is 10. If k is the maximum possible value, then d would be O(log<sub>b</sub>(k)). So overall time complexity is O((n+b) * logb(k))
70+
71+
### More on this topic
72+
- [Radix Sort - WikiPedia](https://en.wikipedia.org/wiki/Radix_sort)
73+
- [Radix Sort - HackerEarth Tutorial](https://www.hackerearth.com/practice/algorithms/sorting/radix-sort/tutorial/)
74+
- [Radix Sort - geeksforgeeks](http://www.geeksforgeeks.org/radix-sort/)
20.2 KB
Loading

Sorting/Radix Sort/radix-sort.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/* Radix Sort implementation in JavaScript */
2+
3+
function radixSort(arr) {
4+
var maxDigit = getMaxDigitLen(arr);
5+
6+
//Looping for every digit index from leftmost digits to rightmost digits
7+
for (var i = 1; i <= maxDigit; i++) {
8+
9+
// rebuild the digit buckets according to this digit
10+
var digitBuckets = [];
11+
for (var j = 0; j < arr.length; j++) {
12+
var digit = getDigit(arr[j], i); //get the i-th digit of the j-th element of the array
13+
14+
digitBuckets[digit] = digitBuckets[digit] || []; //If empty initialize with empty array
15+
digitBuckets[digit].push(arr[j]); //Add the number in it's respective digits bucket
16+
}
17+
18+
// rebuild the array according to this digit
19+
var index = 0;
20+
for (var k = 0; k < digitBuckets.length; k++) {
21+
if (digitBuckets[k] && digitBuckets[k].length > 0) {
22+
for (j = 0; j < digitBuckets[k].length; j++) {
23+
arr[index++] = digitBuckets[k][j];
24+
}
25+
}
26+
}
27+
}
28+
return arr;
29+
}
30+
31+
// helper function to get the last n-th(position) digit of a number
32+
var getDigit = function(num, position) {
33+
var num_length = num.toString().length;
34+
35+
if (num_length < position)
36+
return 0;
37+
38+
var radixPosition = Math.pow(10, position - 1); //number with the positions-th power of 10
39+
40+
/*
41+
Logic: To find the 2nd digit of the number 123
42+
we can divide it by 10, which is 12
43+
and the using the modulus operator(%) we can find 12 % 10 = 2
44+
*/
45+
var digit = (Math.floor(num / radixPosition)) % 10;
46+
47+
return digit;
48+
};
49+
50+
// helper function get the length of digits of the max value in this array
51+
var getMaxDigitLen = function(arr) {
52+
var maxVal = Math.max.apply(Math, arr); //Finding max value from the array
53+
54+
return Math.ceil(Math.log10(maxVal));
55+
};
56+
57+
/******************* Testing Radix sort algorithm *********************/
58+
59+
/**
60+
* Returns a random integer between min (inclusive) and max (inclusive)
61+
* Using Math.round() will give you a non-uniform distribution!
62+
*/
63+
function getRandomInt(min, max) {
64+
return Math.floor(Math.random() * (max - min + 1)) + min;
65+
}
66+
67+
var arr = [];
68+
69+
for (var i = 0; i < 10; i++) { //initialize a random integer unsorted array
70+
arr.push(getRandomInt(1, 100));
71+
}
72+
73+
console.log("Unsorted array: ");
74+
console.log(arr); //printing unsorted array
75+
76+
arr = radixSort(arr);
77+
console.log("Sorted array: ");
78+
console.log(arr); //printing sorted array

0 commit comments

Comments
 (0)