Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <limits.h>
  4.  
  5. #define digit(numm,i) ( ( numm ) >> ( ( i ) * CHAR_BIT ) & UCHAR_MAX )
  6.  
  7. void MSD_radix_sort_r ( int a[], int aux[], int first, int last, int d )
  8. {
  9. if ( d >= 0 )
  10. {
  11. int i, count[UCHAR_MAX + 2] = {0};
  12.  
  13. for ( i = first; i <= last; i++ )
  14. {
  15. count[digit ( a[i], d ) + 1]++;
  16. }
  17. for ( i = 1; i <= UCHAR_MAX; i++ )
  18. {
  19. count[i] += count[i - 1];
  20. }
  21. for ( i = first; i <= last; i++ )
  22. {
  23. aux[count[digit ( a[i], d )]++] = a[i];
  24. }
  25. for ( i = first; i <= last; i++ )
  26. {
  27. a[i] = aux[i - first];
  28. }
  29.  
  30. MSD_radix_sort_r ( a, aux, first, first + count[0] - 1, d - 1 );
  31.  
  32. for (i = 0; i < 4; i++)
  33. {
  34. MSD_radix_sort_r ( a, aux, first + count[i], first + count[i + 1] - 1, d - 1 );
  35. }
  36.  
  37. }
  38. }
  39. void MSD_radix_sort ( int a[], int first, int last )
  40. {
  41. int d = sizeof ( int ) - 1;
  42. int* aux = new int( ( last - first + 1 ) * sizeof ( int ) );
  43.  
  44. MSD_radix_sort_r ( a, aux, first, last, d );
  45.  
  46. delete ( aux );
  47. }
  48.  
  49.  
  50. int main ( void )
  51. {
  52. int a[11] = {12,324,214,412,31312,312312,12312,123,1,2324,123213};
  53. MSD_radix_sort( a, 0, 10 );
  54. for(int i =0; i<11; i++) {
  55. printf("%d, ", a[i]);
  56. }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement