|
| 1 | +//============================================================================ |
| 2 | +// Name : radix-sort.cpp |
| 3 | +// Author : Derek Wene |
| 4 | +// Date : |
| 5 | +// Copyright : Sure, lets say its copyrighted. |
| 6 | +// Description : Implementation of radix sort in C++ |
| 7 | +//============================================================================ |
| 8 | + |
| 9 | +#include "sort.h" |
| 10 | +#include <iostream> |
| 11 | +#include <cmath> |
| 12 | +#include <ostream> |
| 13 | +void |
| 14 | +RadixSort::sort(int A[], int size) |
| 15 | +{ |
| 16 | +int digits=0; |
| 17 | +int x=0; |
| 18 | +for(int i =0; i<size; ++i)//Find largest number |
| 19 | + { |
| 20 | + if(A[i]>digits) |
| 21 | + digits=A[i]; |
| 22 | + |
| 23 | + }//log10(digits);//find highest digit number |
| 24 | + std::cout<<"Largest digit found\n"; |
| 25 | +int B[10]; |
| 26 | +int C[size]; |
| 27 | +int D[size]; |
| 28 | +int power=1; |
| 29 | + |
| 30 | +//while(power<digits)//Get all the digits in the array |
| 31 | + //{ |
| 32 | + |
| 33 | + |
| 34 | + |
| 35 | + |
| 36 | +for(int a=0; a<size; a++) |
| 37 | + { |
| 38 | + C[a]=0; |
| 39 | + D[a]=0; |
| 40 | + } |
| 41 | +for(int b=0; b<10; b++) |
| 42 | + { |
| 43 | + B[b]=0; |
| 44 | + } |
| 45 | + for(int j=0; j<size; ++j)//Set digits |
| 46 | + { |
| 47 | + D[j]=(A[j]/(power))%10; |
| 48 | + std::cout<<D[j]; |
| 49 | + } |
| 50 | + std::cout<<"\nDone making digits file\n"; |
| 51 | + //Counting sort starts here |
| 52 | + for(int k=0; k<size; ++k)//Set number of each digit. |
| 53 | + { |
| 54 | + ++B[D[k]]; |
| 55 | + } |
| 56 | + std::cout<<"\nDone first part B\n"; |
| 57 | + for(int l=1; l<10; ++l)//Do the less than equal part. |
| 58 | + { |
| 59 | + B[l]=B[l]+B[l-1]; |
| 60 | + std::cout<<B[l]<<" "; |
| 61 | + } |
| 62 | + std::cout<<"Done second part B\n"; |
| 63 | + int fix=size-1; |
| 64 | + for(int m=0; m<size; ++m)//Actual Sorting Here |
| 65 | + { |
| 66 | + C[B[D[m]]-1]=A[m]; |
| 67 | + --B[D[m]]; |
| 68 | + std::cout<<"m="<<m<<'\n'; |
| 69 | + } |
| 70 | + std::cout<<"real sorting done\n"; |
| 71 | + for(int n=0; n<size; ++n)//reset A with new sorted part |
| 72 | + { |
| 73 | + A[n]=C[n]; |
| 74 | + } |
| 75 | + std::cout<<"refeed done\n"; |
| 76 | + |
| 77 | + |
| 78 | + |
| 79 | + |
| 80 | + // if(digits==power)//Detects if done with digits |
| 81 | + // break; |
| 82 | + // power*=10; |
| 83 | + //} |
| 84 | + |
| 85 | +} |
0 commit comments