Created
July 12, 2020 13:56
-
-
Save codertcet111/e7496bc916f1e8ba372d6778993170c1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void radix(int array1[], int n){ | |
int buckets_container[10][10]; //This is the buckets shown in the diagram | |
int count[10]; | |
int numberOfIteration, largestNumber, div, bucketnumber, i, j, k; | |
//Now FInd the largest number | |
largestNumber = array1[0]; | |
for(i=1; i<n;i++) | |
if (array1[i] > largestNumber) | |
largestNumber = array1[i]; | |
//FInd the number of digits in the largestNumber number | |
numberOfIteration = 0; | |
while(largestNumber > 0){ | |
numberOfIteration++; | |
largestNumber = largestNumber/10; | |
} | |
//Now its time for radix sort | |
div = 1; | |
for(i = 1;i <= numberOfIteration; i++) | |
//Initialize bucket | |
for(j=0;j<=9;j++) | |
count[j]=0; | |
//Insert element in the respective bucket | |
for(j=0;j<n;j++) | |
bucketnumber = (array1[j]/div)%10; | |
buckets_container[bucketnumber][count[bucketnumber]] = array1[j]; | |
count[bucketnumber]++; | |
//Now its time to collect all the bucket elements in an array | |
j=0; | |
for(bucketnumber = 0;bucketnumber <= 9;bucketnumber++) | |
for(k=0; k < count[bucketnumber]; k++) | |
array1[j++]=buckets_container[bucketnumber][k]; | |
div = div * 10; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment