Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Radix
//g++ 7.4.0 //Radix Sort //credit Geeks For Geeks #include <iostream> using namespace std; int getMax(int a[], int n) { int max = a[0]; for (int i = 1; i < n; i++) if (a[i] > max) max = a[i]; return max; } void countSort(int a[], int n, int exp) { int output[n]; int i, count[10] = { 0 }; for (i = 0; i < n; i++) count[(a[i] / exp) % 10]++; for (i = 1; i < 10; i++) count[i] += count[i - 1]; for (i = n - 1; i >= 0; i--) { output[count[(a[i] / exp) % 10] - 1] = a[i]; count[(a[i] / exp) % 10]--; } for (i = 0; i < n; i++) a[i] = output[i]; } void radSort(int a[], int n) { int m = getMax(a, n); for (int exp = 1; m / exp > 0; exp *= 10) countSort(a, n, exp); } void print(int a[], int n) { for (int i = 0; i < n; i++) cout << a[i] << " "; } int main() { int a[] = { 67,89,55,44,23,21,30,40,92,94 }; int n = sizeof(a) / sizeof(a[0]); radSort(a, n); print(a, n); return 0; }
run
|
edit
|
history
|
help
0
Perf measurement
maximum nights you can accommodate
Comp Conexe
inorder traversal
cpp ex 3 - solution
Patara asoebi
Policy based smart pointer
Kalkulator z bajerami
Estocasticos V1.1
c++_array_size