Skip to content

Commit 3fed19c

Browse files
sergobotsinghpratyush
authored andcommitted
Fix #567: Add Radix Sort [C] (#571)
* Added Radix Sort [C] * Use tabs instead of spaces and add final newline * Add entry to README.md * Fix a silly silly bug
1 parent af47a99 commit 3fed19c

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ Community (college) maintained list of Algorithms and Data Structures implementa
4242
| [Prims](https://en.wikipedia.org/wiki/Prim%27s_algorithm) | [:white_check_mark:](prims/prims.c) | | [:white_check_mark:](prims/Prims.java) | | [:white_check_mark:](prims/prims.go) | [:white_check_mark:](prims/prims.js) | |
4343
| [Quick Select](https://en.wikipedia.org/wiki/Quickselect) | [:white_check_mark:](quick_select/quick_select.c) | | [:white_check_mark:](quick_select/QuickSelect.java) | [:white_check_mark:](quick_select/quick_select.py) | | | |
4444
| [Quicksort](https://en.wikipedia.org/wiki/Quicksort) | [:white_check_mark:](quick_sort/quicksort.c) | | [:white_check_mark:](quick_sort/QuickSort.java) | [:white_check_mark:](quick_sort/quick_sort.py) | [:white_check_mark:](quick_sort/quick_sort.go) | [:white_check_mark:](quick_sort/quickSort.js) | [:white_check_mark:](quick_sort/QuickSort.cs) |
45-
| [Radix Sort](http://www.geeksforgeeks.org/radix-sort/) | | | [:white_check_mark:](radix_sort/RadixSort.java) | [:white_check_mark:](radix_sort/radix_sort.py) | | | |
45+
| [Radix Sort](http://www.geeksforgeeks.org/radix-sort/) | [:white_check_mark:](radix_sort/radix_sort.c) | | [:white_check_mark:](radix_sort/RadixSort.java) | [:white_check_mark:](radix_sort/radix_sort.py) | | | |
4646
| [Rod Cutting Problem](http://www.geeksforgeeks.org/dynamic-programming-set-13-cutting-a-rod/) | [:white_check_mark:](rod_cutting_problem/rod_cutting.c) | | [:white_check_mark:](rod_cutting_problem/RodCutting.java) | [:white_check_mark:](rod_cutting_problem/rod_cutting.py) | [:white_check_mark:](rod_cutting_problem/rod_cutting.go) | [:white_check_mark:](rod_cutting_problem/rodCuttingProblem.js) | |
4747
| [Shell Sort](https://en.wikipedia.org/wiki/Shellsort) | | [:white_check_mark:](shell_sort/ShellSort.cpp) | [:white_check_mark:](shell_sort/ShellSort.java) | [:white_check_mark:](/shell_sort/shell_sort.py) | [:white_check_mark:](shell_sort/shell_sort.go) | [:white_check_mark:](shell_sort/shellSort.js) | [:white_check_mark:](shell_sort/ShellSort.cs) |
4848
| [Sieve of Eratosthenes](https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes) | [:white_check_mark:](sieve_of_eratosthenes/sieveOfEratosthenes.c) | | [:white_check_mark:](sieve_of_eratosthenes/SieveOfEratosthenes.java) | [:white_check_mark:](sieve_of_eratosthenes/sieve_of_eratosthenes.py) | [:white_check_mark:](sieve_of_eratosthenes/sieve_of_eratosthenes.go) | [:white_check_mark:](sieve_of_eratosthenes/sieveOfEratosthenes.js) | |

radix_sort/radix_sort.c

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#include "stdio.h"
2+
#include <time.h>
3+
#include <stdlib.h>
4+
5+
/*
6+
* Returns maximum number in an array
7+
* @a array pointer
8+
* @n number of elements in the array
9+
*/
10+
int max(int * a, int n) {
11+
int max = a[0];
12+
for (int i = 1; i < n; i++) {
13+
if (a[i] > max) {
14+
max = a[i];
15+
}
16+
}
17+
return max;
18+
}
19+
20+
/*
21+
* Performs Radix Sort on a given array
22+
* @a array pointer
23+
* @n number of elements in the array
24+
*/
25+
void radix_sort(int * a, int n) {
26+
int m = max(a, n);
27+
int b[10] = {0};
28+
29+
for (int exp = 1; m / exp > 0; exp*=10) {
30+
int bucket[10] = {0};
31+
32+
for (int i = 0; i < n; i++)
33+
bucket[(a[i] / exp) % 10]++;
34+
for (int i = 1; i < n; i++)
35+
bucket[i] += bucket[i - 1];
36+
for (int i = n - 1; i >= 0; i--)
37+
b[--bucket[(a[i] / exp) % 10]] = a[i];
38+
39+
for (int i = 0; i < n; i++)
40+
a[i] = b[i];
41+
}
42+
}
43+
44+
int main() {
45+
srand(time(NULL));
46+
int n = 10;
47+
48+
int test_array[n];
49+
for (int i = 0; i < n; i++) {
50+
test_array[i] = rand() % 1000;
51+
}
52+
53+
radix_sort(test_array, n);
54+
for (int i = 0; i < n; i++) {
55+
printf("%d ", test_array[i]);
56+
}
57+
printf("\n");
58+
return 0;
59+
}

0 commit comments

Comments
 (0)