Advertisement
Guest User

fsdg

a guest
Mar 28th, 2020
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.63 KB | None | 0 0
  1.  #include<stdio.h>
  2.  
  3.      
  4.  
  5.     int getMax(int arr[], int n) {
  6.  
  7.         int mx = arr[0];
  8.  
  9.         int i;
  10.  
  11.         for (i = 1; i < n; i++)
  12.  
  13.             if (arr[i] > mx)
  14.  
  15.                 mx = arr[i];
  16.  
  17.         return mx;
  18.  
  19.     }
  20.  
  21.      
  22.  
  23.     void countSort(int arr[], int n, int exp) {
  24.  
  25.         int output[n]; // output array
  26.  
  27.         int i, count[10] = { 0 };
  28.  
  29.      
  30.  
  31.         // Store count of occurrences in count[]
  32.  
  33.         for (i = 0; i < n; i++)
  34.  
  35.             count[(arr[i] / exp) % 10]++;
  36.  
  37.      
  38.  
  39.         for (i = 1; i < 10; i++)
  40.  
  41.             count[i] += count[i - 1];
  42.  
  43.      
  44.  
  45.         // Build the output array
  46.  
  47.         for (i = n - 1; i >= 0; i--) {
  48.  
  49.             output[count[(arr[i] / exp) % 10] - 1] = arr[i];
  50.  
  51.             count[(arr[i] / exp) % 10]--;
  52.  
  53.         }
  54.  
  55.      
  56.  
  57.         for (i = 0; i < n; i++)
  58.  
  59.             arr[i] = output[i];
  60.  
  61.     }
  62.  
  63.      
  64.  
  65.     // The main function to that sorts arr[] of size n using Radix Sort
  66.  
  67.     void radixsort(int arr[], int n) {
  68.  
  69.         int m = getMax(arr, n);
  70.  
  71.      
  72.  
  73.         int exp;
  74.  
  75.         for (exp = 1; m / exp > 0; exp *= 10)
  76.  
  77.             countSort(arr, n, exp);
  78.  
  79.     }
  80.  
  81.      
  82.  
  83.     void print(int arr[], int n) {
  84.  
  85.         int i;
  86.  
  87.         for (i = 0; i < n; i++)
  88.  
  89.             printf("%d ", arr[i]);
  90.  
  91.     }
  92.  
  93.      
  94.  
  95.     int main() {
  96.  
  97.         int Z;
  98.         scanf("%d", &Z);
  99.  
  100.     while (Z--) {
  101.  
  102.         int n, length;
  103.         scanf("%d", &n);
  104.             scanf("%d", &length);
  105.         int tab[n];
  106.         for(int i=0; i<n; i++){
  107.                 scanf("%d", &tab[i]);
  108.         }
  109.  
  110.         radixsort(tab, n);
  111.  
  112.         print(tab, n);
  113.  
  114.         return 0;
  115.  
  116.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement