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
std::function copies
Te
Graphs Iteration 2.1 Directed Graphs
DESim Example with Hash Table Starter Code
hello world 3
nontype template parameter produced with decltype for function
Unlike C (even C99/C11), C++ allows initializers in if-conditions, so this compiles.
Alternation regex
lref assignment
std::reference_wrapper