Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdlib>
- #include <math.h>
- using namespace std;
- int *list;
- int digit;
- int **bins;
- int counters[10];
- int pass;
- void radix_sort(int size, int numLength);
- void show_bins();
- int get_digit(int number, int digit);
- /**
- * <b>int main()</b></br>
- * <i>Sets up and runs the program</i></br>
- */
- int main() {
- radix_sort(4, 3);
- return 0;
- }
- /**
- * <b>void radix_sort(int size)</b></br>
- * <i>A dynamic memory version of a basic radix sort.</i></br>
- *
- * @param size The size of the array
- */
- void radix_sort(int size, int numLength) {
- // Set up arrays
- list = new int[size];
- bins = new int*[10];
- for (int i = 0; i < 10; i++) bins[i] = new int[size];
- // Have user enter values
- for (int i = 0; i < size; i++) {
- cout << "[Input]: ";
- cin >> list[i];
- }
- int place = 0;
- // Pass through all of the actions 'numLength' amount of times
- for (int len = 0; len < numLength; len++) {
- printf("\n\n[Outer Pass]\n\ | Pass Count: %d\n\ | Boolean Expression: (%d < %d)\n\ | \n", len, len, numLength);
- printf("\ |\t[Alert] Resetting counters and filling bins.\n\ |\t|\n");
- for (int i = 0; i < 10; i++) counters[i] = 0;
- // Filling Bins
- for (int i = 0; i < size; i++) {
- digit = get_digit(list[i], len + 1);
- counters[digit] += 1;
- bins[digit][counters[digit - 1]] = list[i];
- printf("\ |\t| [Bin Pass]\n\ |\t|\t|\n");
- printf("\ |\t|\t| Digit Number: %d\n", len + 1);
- printf("\ |\t|\t| List Element: %d\n", list[i]);
- printf("\ |\t|\t| Digit Aquired: %d\n\ |\t|\n", digit);
- }
- printf("\ |\t[Alert] Done filling bins.\n\ |\n");
- printf("\ |\t[Alert] Dumping array.\n");
- printf("\ |\t| [Array/Bin Alteration]\n\ |\t|\t|\n");
- show_bins();
- // Dumping array and emptying bins
- for (int k = 0; k < 10; k++) {
- printf("\ |\t|\t| Bin: %d\n", k);
- printf("\ |\t|\t| Counter Value: %d\n", counters[k]);
- for (int j = 0; j < counters[k]; j++) {
- if (counters[k] > 0) {
- printf("\ |\t|\t|\t|\n");
- printf("\ |\t|\t|\t| List Place: %d\n", place);
- printf("\ |\t|\t|\t| From Bin: bins[k(%d)][j(%d)] = %d\n", k, j, bins[k][j]);
- printf("\ |\t|\t|\t|\n");
- list[place] = bins[k][j];
- printf("\ |\t|\t|\t| Value at Place: %d\n", list[place]);
- printf("\ |\t|\t|\t| \ |\ [Alert] Clearing bins[k][j]\n");
- bins[k][j] = 0;
- place++;
- }
- }
- printf("\ |\t|\t|\n");
- }
- for (int i = 0; i < size; i++) cout << list[i] << endl;
- show_bins();
- //if (len == 1) exit(0);
- }
- for (int i = 0; i < size; i++) cout << list[i] << endl;
- delete list;
- delete bins;
- }
- /**
- * <b>void show_bins()</b></br>
- * <i>Shows bin contents at time of function execution.</i></br>
- */
- void show_bins() {
- for (int a = 0; a < 10; a++) {
- printf("[BIN %d] [COUNTER %d]", a, counters[a]);
- for (int b = 0; b < counters[a]; b++) {
- printf(" %d,", bins[a][b]);
- }
- printf("\n");
- }
- printf("\n");
- }
- int get_digit(int number, int digit) {
- return (number / (int) pow(10, digit - 1)) % 10;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement