Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- void radixsort(int *a, int n){
- int i, m = a[0], exp = 1;
- int *temp = (int *) malloc(sizeof(int) * n); //temp array
- for(i = 1; i < n; i++) if (a[i] > m) m = a[i]; //find maximum element
- while (m / exp > 0){
- int digit[10] ={0}; //initialize all elements in digit[] to 0
- for(i = 0; i < n; i++) digit[(a[i] / exp) % 10]++; //add 1 corresponding to index of digit
- for(i = 1; i < 10; i++) digit[i] += digit[i - 1]; //keep on adding from left to right
- for(i = n - 1; i >= 0; i--) temp[--digit[(a[i] / exp) % 10]] = a[i]; //put number in temp
- for(i = 0; i < n; i++) a[i] = temp[i]; //copy from temp[] to a[]
- exp *= 10;
- }
- }
- int main(){
- int i, n, *arr;
- printf("Enter total elements: ");
- scanf("%d", &n);
- arr = (int *) malloc(sizeof(int) * n);
- printf("Enter %d Elements : ", n);
- for (i = 0; i < n; i++) scanf("%d", &arr[i]);
- radixsort(&arr[0], n);
- for(i = 0; i<n; i++) printf("%d\t", arr[i]);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement