Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
radixSort
//clang 3.8.0 #include <iostream> void countingSort(int *A,int len,int place ){ int output[len+1]; int max=(A[0]/place)%10; for(int i=1;i<len;++i ){ if((A[i]/place)%10>max){max=A[i];} } int count[max+1]; for(int i=0;i<max;++i ){ count[i]=0; } for(int i=0;i<len;++i ){ ++count[(A[i]/place)%10]; } for(int i=1;i<10;++i ){ count[i]+=count[i-1]; } for(int i=len-1;i>=0;--i ){ output[count[(A[i]/place)%10]-1]=A[i]; --count[(A[i]/place)%10]; } for(int i=0;i<len;++i ){ A[i]=output[i]; } } int getMax(int *A,int len){ int max=A[0]; for(int i=1;i<len;++i ){ if(A[i]>max)max=A[i]; } return max; } void radixSort(int *A,int len){ int max=getMax(A,len); for(int place=1;max/place>0;place*=10 ){ countingSort(A,len,place); } } int main() { int A[]{9,867,431,95,2,1,839,0,6,2}; int len=sizeof(A)/sizeof(A[0]); radixSort(A,len); for(int i:A ) std::cout <<i<<' '; }
run
|
edit
|
history
|
help
0
ternary test c++14
Wrapper to pass shared_ptr to &, const &, *, const * through std::bind.
Standard Template Library
user defined exception
C++ Register
What's the problem with this?
test
Tilted uniform distribution random number generator over min/max range
Last Class Quiz - Working with Hash Table
Integer conversions