Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added radix_sort.c and Modified Readme.md #1548

Merged
merged 1 commit into from
Oct 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Sorting/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
| [Insertion Sort](https://en.wikipedia.org/wiki/Insertion_sort) | [:heavy_check_mark:](Insertion%20Sort/C/insertion_sort.c) | [:heavy_check_mark:](Insertion%20Sort/C%2B%2B/insertion_sort.cpp) | [:heavy_check_mark:](Insertion%20Sort/Java/insertion_sort.java) | [:heavy_check_mark:](Insertion%20Sort/Python/insertion_sort.py) | [:heavy_check_mark:](Insertion%20Sort/Javascript/insertion_sort.js) | | [:heavy_check_mark:](Insertion%20Sort/Go/insertion_sort.go) | [:heavy_check_mark:](Insertion%20Sort/Ruby/insertion_sort.rb) | [:heavy_check_mark:](Insertion%20Sort/Rust/insertion_sort.rs) | |
| [Merge Sort](https://en.wikipedia.org/wiki/Merge_sort) | [:heavy_check_mark:](Merge%20Sort/C/merge_sort.c) | [:heavy_check_mark:](Merge%20Sort/C%2B%2B/merge_sort.cpp) | [:heavy_check_mark:](Merge%20Sort/Java/merge_sort.java) | [:heavy_check_mark:](Merge%20Sort/Python/merge_sort.py) | [:heavy_check_mark:](Merge%20Sort/Javascript/merge_sort.js) | | | [:heavy_check_mark:](Merge%20Sort/Ruby/merge_sort.rb) | [:heavy_check_mark:](Merge%20Sort/Rust/merge-sort.rs) | [:heavy_check_mark:](Merge%20Sort/Swift/merge_sort.swift) |
| [Quick Sort](https://en.wikipedia.org/wiki/Quick_sort) | [:heavy_check_mark:](quickSort/C/quick_sort.c) | [:heavy_check_mark:](quickSort/C%2B%2B/quick_sort.cpp) | [:heavy_check_mark:](quickSort/Java/quick_sort.java) | [:heavy_check_mark:](quickSort/python/quick_sort.py) | [:heavy_check_mark:](quickSort/Javascript/quick_sort.js) | [:heavy_check_mark:](quickSort/csharp/QuickSort.cs) | [:heavy_check_mark:](quickSort/Go/quick_sort.go) | [:heavy_check_mark:](quickSort/Ruby/quick_sort.rb) | [:heavy_check_mark:](quickSort/Rust/quick-sort.rs) | |
| [Radix Sort](https://en.wikipedia.org/wiki/Radix_sort) | | [:heavy_check_mark:](Radix%20Sort/C%2B%2B/radix_sort.cpp) | [:heavy_check_mark:](Radix%20Sort/Java/radix_sort.java) | [:heavy_check_mark:](Radix%20Sort/python/Radix_Sort.py) | | | [:heavy_check_mark:](Radix%20Sort/Go/radix_sort.go) | [:heavy_check_mark:](Radix%20Sort/Ruby/radixSort.rb) | | |
| [Radix Sort](https://en.wikipedia.org/wiki/Radix_sort) |[:heavy_check_mark:](Radix%20Sort/C/radix_sort.c) | [:heavy_check_mark:](Radix%20Sort/C%2B%2B/radix_sort.cpp) | [:heavy_check_mark:](Radix%20Sort/Java/radix_sort.java) | [:heavy_check_mark:](Radix%20Sort/python/Radix_Sort.py) | | | [:heavy_check_mark:](Radix%20Sort/Go/radix_sort.go) | [:heavy_check_mark:](Radix%20Sort/Ruby/radixSort.rb) | | |
| [Selection Sort](https://en.wikipedia.org/wiki/Selection_sort) | [:heavy_check_mark:](Selection%20Sort/C/selection_sort.c) | [:heavy_check_mark:](Selection%20Sort/C%2B%2B/selection_sort.cpp) | [:heavy_check_mark:](Selection%20Sort/Java/selection_sort.java) | [:heavy_check_mark:](Selection%20Sort/Python/selection_sort.py) | [:heavy_check_mark:](Selection%20Sort/JavaScript/selectionSort.js) | | [:heavy_check_mark:](Selection%20Sort/Go/selection_sort.go) | [:heavy_check_mark:](Selection%20Sort/Ruby/selSort.rb) | [:heavy_check_mark:](Selection%20Sort/Rust/selection_sort.rs) | |
| [Shell Sort](https://en.wikipedia.org/wiki/Shellsort) | [:heavy_check_mark:](ShellSort/c/shell_sort.c) | [:heavy_check_mark:](ShellSort/C%2B%2B/shell_sort.cpp) | [:heavy_check_mark:](ShellSort/Java/ShellSort.java) | [:heavy_check_mark:](ShellSort/python/shell_sort.py) | [:heavy_check_mark:](ShellSort/Javascript/shellsort.js) | [:heavy_check_mark:](ShellSort/csharp/ShellSort.cs) | | [:heavy_check_mark:](ShellSort/Ruby/shellSort.rb) | | |
79 changes: 79 additions & 0 deletions Sorting/Radix Sort/C/radix_sort.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*Radix sort
A sorting technique that sorts the elements by first grouping the individual digits of the same place value.
Then, sort the elements according to their increasing/decreasing order.*/

#include <stdio.h>

int max(int num[], int n);

void radix_sort(int num[], int n);

int main()
{
int i, n;
printf("Enter the number of elements: ");
scanf("%d", &n);
int num[n];
printf("Enter the numbers: ");
for (i = 0; i < n; i++)
{
scanf("%d", &num[i]);
}
radix_sort(num, n);

printf("\n The Sorted list is : ");

for (i = 0; i < n; i++)
printf("%d ", num[i]);

printf("\n");
return 0;
}

int max(int num[], int n)
{
int max = num[0];
for (int i = 1; i < n; i++)
if (num[i] > max)
max = num[i];
return max;
}

void radix_sort(int num[], int n)
{
int box[10][10], box_cnt[10];
int i, j, k, r, NOP = 0, divisor = 1, lar, pass;
lar = max(num, n);
while (lar > 0)
{
NOP++;
lar /= 10;
}
for (pass = 0; pass < NOP; pass++)
{
for (i = 0; i < 10; i++)
{
box_cnt[i] = 0;
}
for (i = 0; i < n; i++)
{
r = (num[i] / divisor) % 10;
box[r][box_cnt[r]] = num[i];
box_cnt[r] += 1;
}
i = 0;
for (k = 0; k < 10; k++)
{
for (j = 0; j < box_cnt[k]; j++)
{
num[i] = box[k][j];
i++;
}
}
divisor *= 10;
printf("\tAfter Pass %d : ", pass + 1);
for (i = 0; i < n; i++)
printf("%d ", num[i]);
printf("\n");
}
}