Advertisement
Guest User

Radix Sort

a guest
Feb 15th, 2014
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.12 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <math.h>
  4. using namespace std;
  5.  
  6. int *list;
  7. int digit;
  8.  
  9. int **bins;
  10. int counters[10];
  11.  
  12. int pass;
  13.  
  14. void radix_sort(int size, int numLength);
  15. void show_bins();
  16. int get_digit(int number, int digit);
  17.  
  18. /**
  19. *  <b>int main()</b></br>
  20. *  <i>Sets up and runs the program</i></br>
  21. */
  22. int main() {
  23.    
  24.     radix_sort(4, 3);
  25.    
  26.     return 0;
  27. }
  28.  
  29. /**
  30. *  <b>void radix_sort(int size)</b></br>
  31. *  <i>A dynamic memory version of a basic radix sort.</i></br>
  32. *  
  33. *  @param size The size of the array
  34. */
  35. void radix_sort(int size, int numLength) {
  36.    
  37.     // Set up arrays
  38.     list = new int[size];
  39.     bins = new int*[10];
  40.     for (int i = 0; i < 10; i++) bins[i] = new int[size];
  41.    
  42.     // Have user enter values
  43.     for (int i = 0; i < size; i++) {
  44.         cout << "[Input]: ";
  45.         cin >> list[i];
  46.     }
  47.    
  48.     int place = 0;
  49.    
  50.     // Pass through all of the actions 'numLength' amount of times
  51.     for (int len = 0; len < numLength; len++) {
  52.        
  53.         printf("\n\n[Outer Pass]\n\ | Pass Count: %d\n\ | Boolean Expression: (%d < %d)\n\ | \n", len, len, numLength);
  54.         printf("\ |\t[Alert] Resetting counters and filling bins.\n\ |\t|\n");
  55.        
  56.         for (int i = 0; i < 10; i++) counters[i] = 0;
  57.        
  58.         // Filling Bins
  59.         for (int i = 0; i < size; i++) {
  60.  
  61.             digit = get_digit(list[i], len + 1);
  62.             counters[digit] += 1;
  63.             bins[digit][counters[digit - 1]] = list[i];
  64.            
  65.             printf("\ |\t| [Bin Pass]\n\ |\t|\t|\n");
  66.             printf("\ |\t|\t|  Digit Number: %d\n", len + 1);
  67.             printf("\ |\t|\t|  List Element: %d\n", list[i]);
  68.             printf("\ |\t|\t|  Digit Aquired: %d\n\ |\t|\n", digit);
  69.         }
  70.        
  71.         printf("\ |\t[Alert] Done filling bins.\n\ |\n");
  72.         printf("\ |\t[Alert] Dumping array.\n");
  73.         printf("\ |\t| [Array/Bin Alteration]\n\ |\t|\t|\n");
  74.        
  75.         show_bins();
  76.        
  77.         // Dumping array and emptying bins
  78.         for (int k = 0; k < 10; k++) {
  79.            
  80.             printf("\ |\t|\t|  Bin: %d\n", k);
  81.             printf("\ |\t|\t|  Counter Value: %d\n", counters[k]);
  82.            
  83.             for (int j = 0; j < counters[k]; j++) {
  84.                
  85.                 if (counters[k] > 0) {
  86.                    
  87.                     printf("\ |\t|\t|\t|\n");
  88.                     printf("\ |\t|\t|\t|  List Place: %d\n", place);
  89.                     printf("\ |\t|\t|\t|  From Bin: bins[k(%d)][j(%d)] = %d\n", k, j, bins[k][j]);
  90.                     printf("\ |\t|\t|\t|\n");
  91.                     list[place] = bins[k][j];
  92.                    
  93.                     printf("\ |\t|\t|\t|  Value at Place: %d\n", list[place]);
  94.                     printf("\ |\t|\t|\t|  \ |\ [Alert] Clearing bins[k][j]\n");
  95.                     bins[k][j] = 0;
  96.                    
  97.                     place++;
  98.                 }
  99.             }
  100.            
  101.             printf("\ |\t|\t|\n");
  102.         }
  103.        
  104.         for (int i = 0; i < size; i++) cout << list[i] << endl;
  105.        
  106.         show_bins();
  107.         //if (len == 1) exit(0);
  108.     }
  109.    
  110.     for (int i = 0; i < size; i++) cout << list[i] << endl;
  111.    
  112.     delete list;
  113.     delete bins;
  114. }
  115.  
  116. /**
  117. *  <b>void show_bins()</b></br>
  118. *  <i>Shows bin contents at time of function execution.</i></br>
  119. */
  120. void show_bins() {
  121.    
  122.     for (int a = 0; a < 10; a++) {
  123.        
  124.         printf("[BIN %d] [COUNTER %d]", a, counters[a]);
  125.        
  126.         for (int b = 0; b < counters[a]; b++) {
  127.             printf(" %d,", bins[a][b]);
  128.         }
  129.        
  130.         printf("\n");
  131.     }
  132.    
  133.     printf("\n");
  134. }
  135.  
  136. int get_digit(int number, int digit) {
  137.     return (number / (int) pow(10, digit - 1)) % 10;
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement