SHARE
TWEET

Untitled

a guest Apr 19th, 2019 79 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. void radix_sort(int arr[], int SIZE, int base)
  4. {
  5.     int i, j = 0, max, iteration = 1, *bucket, *semisorted;
  6.     max = find_max(arr, SIZE);
  7.  
  8.     semisorted = (int *)malloc(sizeof(int)*SIZE);
  9.     if (semisorted == NULL)
  10.         exit(1);
  11.     while (max/iteration!=0)
  12.     {
  13.         bucket = (int *)calloc(sizeof(int), SIZE);
  14.        
  15.         if (bucket == NULL)
  16.             exit(1);
  17.         for (i = 0; i < SIZE; i++)
  18.             bucket[(arr[i] / iteration) % 10]++;
  19.         for (i = 1; i < base; i++)
  20.             bucket[i] += bucket[i - 1];
  21.         for (i = SIZE - 1; i >= 0; i--)
  22.             semisorted[--bucket[(arr[i] / iteration) % 10]] = arr[i];
  23.         for (i = 0; i < SIZE; i++)
  24.             arr[i] = semisorted[i];
  25.         iteration *= 10;
  26.         free(bucket);
  27.     }
  28. }
  29.  
  30. int find_max(int arr[], int SIZE)
  31. {
  32.     int i, maxx = -1;
  33.     for (i = 0; i < SIZE; i++)
  34.     {
  35.         if (maxx < arr[i])
  36.             maxx = arr[i];
  37.     }
  38.  
  39.     return maxx;
  40. }
  41. void main()
  42. {
  43.     int temp = 0, i, b, size=0, *ar;
  44.     printf("Please enter a size of the array\n");
  45.    
  46.     do {
  47.         scanf_s("%d", &size);
  48.         printf("The size must be at least 2 numbers\n");
  49.     } while (size<1);
  50.  
  51.     ar = (int *)malloc(sizeof(int)*size);
  52.     if (ar == NULL)
  53.         exit(1);
  54.  
  55.     printf("Please enter a base 2,8 or 10\n");
  56.     do {
  57.         scanf_s("%d", &b);
  58.         printf("Please enter a base 2,8 or 10\n");
  59.     } while (b != 2 && b != 8 && b != 10);
  60.  
  61.     printf("Enter number into array for the same base that you choesd \n");
  62.     for (i = 0; i <size; i++)
  63.     {
  64.         scanf_s("%d", &ar[i]);
  65.         temp = *(ar + i);
  66.         while (temp != 0)
  67.         {
  68.             if (temp % 10 < b)
  69.                 temp /= 10;
  70.             else
  71.             {
  72.                 printf("This number bigger then the base \n");
  73.                 exit(1);
  74.             }      
  75.         }
  76.     }
  77.  
  78.     radix_sort(ar, size, b);
  79.     for (i = 0; i < size; i++)
  80.         printf("%d ", *(ar + i));
  81.     free(ar);
  82. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top