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

radix sort via c programming #43

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
63 changes: 63 additions & 0 deletions SortingAlgorithms/radixSort.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#include <stdio.h>
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try to include a comment containing the Problem statement with Sample Test Cases that have been tested with the code for future PRs. 👨🏻‍💻
Refer : CONTRIBUTING.md

#include <conio.h>
#include <stdlib.h>


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

void countSort(int list[], int n, int exp) {
int output[n];
int i, count[10] = { 0 };

for (i = 0; i < n; i++)
count[(list[i] / exp) % 10]++;

for (i = 1; i < 10; i++)
count[i] += count[i - 1];

for (i = n - 1; i >= 0; i--) {
output[count[(list[i] / exp) % 10] - 1] = list[i];
count[(list[i] / exp) % 10]--;
}

for (i = 0; i < n; i++)
list[i] = output[i];
}

void radixsort(int list[], int n) {
int m = getMax(list, n);

int exp;
for (exp = 1; m / exp > 0; exp *= 10)
countSort(list, n, exp);
}

void print(int list[], int n) {
int i;
for (i = 0; i < n; i++)
printf("%d\t", list[i]);
}

int main()
{
int list[] = { 82, 901, 100, 12, 150, 77, 55, 23 };
int i, n = sizeof(list) / sizeof(list[0]);

printf("List of numbers before sort: \n");
for(i = 0; i<8; i++)
printf("%d\t", list[i] );

radixsort(list, n);

printf("\n\nList of numbers after sort: \n");
print(list, n);
printf("\n\n");
return 0;
}