The ones who are crazy enough to think they can change the world are the ones who do.- Steve Jobs
Radix sort algorithm is a non-comparative integer sorting algorithm. Radix sort algorithm is most preferable for unsorted list. Let us consider the following example to understand clearly about the working of radix sort algorithm to arrage the unsorted list of array. Clearly, the number of iteration is based on size of the highest individual number.
Here is the program to demonstrate Radix Sort.
#include <stdio.h> int largest(int arr[], int n); void radix_sort(int arr[], int n); int main() { int arr[10], i, n; printf("\n Enter the size of the array : "); scanf("%d",&n); printf("\nEnter %d elements in the array : \n",n); for(i = 0; i < n; i++) scanf("%d", &arr[i]); radix_sort(arr, n); printf("\nSorted Array in ascending order is : \n"); for(i = 0; i < n; i++) printf("%d",arr[i]); return 0; } int largest(int arr[], int n) { int large = arr[0], i; for(i = 1; i < n; i++) { if(arr[i] > large) large = arr[i]; } return large; } void radix_sort(int arr[], int n) { int bucket[10][10], bucket_count[10]; int i, j, k, remainder, NOP = 0, divisor = 1, large, pass; large = largest(arr, n); while(large > 0) { NOP++; large /= 10; } for(pass = 0; pass < NOP; pass++) { for(i = 0; i < 10; i++) bucket_count[i] = 0; for(i = 0; i < n; i++) { remainder = (arr[i]/divisor)%10; bucket[remainder][bucket_count[remainder]] = arr[i]; bucket_count[remainder] += 1; } i = 0; for(k = 0; k < 10; k++) { for(j = 0; j < bucket_count[k]; j++) { arr[i] = bucket[k][j]; i++; } } divisor *= 10; } }
Enter the size of the array : 5 Enter 5 elements in the array : 4 3 1 2 5 Sorted Array in ascending order is : 1 2 3 4 5
We may make mistakes(spelling, program bug, typing mistake and etc.), So we have this container to collect mistakes. We highly respect your findings.
© Copyright 2019