Last active
October 9, 2019 13:44
-
-
Save belushkin/976a050166386991e7902a379c503ee2 to your computer and use it in GitHub Desktop.
counting sort impolementation not from memory
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <bits/stdc++.h> | |
int a[] = {9,3,1,4,5,7,7,2,2}; | |
void counting_sort(int a[], int n) { | |
int radix = *std::max_element(a, a+n); | |
int c[radix+1] = {}; | |
int b[n] = {}; | |
for (int i=0; i<n; i++) { | |
c[a[i]]++; | |
} | |
for (int i=1; i<=radix; i++) { | |
c[i] = c[i-1] + c[i]; | |
} | |
for (int i=n-1; i>-1; i--) { | |
int m = a[i]; | |
c[m]--; | |
b[c[m]] = a[i]; | |
} | |
for (int i=0; i<n; i++) { | |
a[i] = b[i]; | |
} | |
} | |
int main() { | |
int n = sizeof(a)/sizeof(a[0]); | |
counting_sort(a, n); | |
for (int i = 0; i<n; i++) { | |
std::cout << a[i] << " "; | |
} | |
std::cout << "\n"; | |
return 1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment