Created
March 8, 2017 07:02
-
-
Save tooshitaka/26c42254b7044f831a5cac19840a2d07 to your computer and use it in GitHub Desktop.
This file contains 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> | |
const int K = 10000; | |
// Countingsort | |
void countingsort(int A[], int B[], int n) | |
{ | |
int C[10001]; | |
// Initialize | |
for (int i = 0; i <= K; i++) | |
C[i] = 0; | |
// Count frequency | |
for (int i = 0; i < n; i++) | |
C[A[i]]++; | |
// Calculate cumulative sum | |
for (int i = 1; i <= K; i++) | |
C[i] = C[i] + C[i - 1]; | |
for (int i = n - 1; i >= 0; i--) { | |
B[C[A[i]] - 1] = A[i]; | |
C[A[i]]--; | |
} | |
} | |
// MAIN FUNCTION | |
int main() | |
{ | |
// Input | |
int n; | |
int A[2000000]; | |
std::cin >> n; | |
A[0] = 0; | |
for (int i = 0; i < n; i++) { | |
std::cin >> A[i]; | |
} | |
// Counting sort | |
int B[2000000]; | |
countingsort(A, B, n); | |
for (int i = 0; i < n; i++) { | |
if (i) | |
std::cout << " "; | |
std::cout << B[i]; | |
} | |
std::cout << std::endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment