Last active
April 2, 2024 16:33
-
-
Save megarubber/0705c87107b8273c1392efd93e9dac4f to your computer and use it in GitHub Desktop.
Counting Sort
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 <stdio.h> | |
#include <stdlib.h> | |
#include <time.h> | |
#define MAX 10000000 | |
void countingsort(int* A, int* B, int size) { | |
int i; | |
int k = A[0]; | |
for(i = 0; i < size; i++) | |
if(A[i] > k) k = A[i]; | |
int C[k + 1]; | |
for(i = 0; i < k; i++) | |
C[i] = 0; | |
for(i = 0; i < size; i++) | |
C[A[i]]++; | |
for(i = 1; i <= k; i++) | |
C[i] += C[i - 1]; | |
for(i = size - 1; i >= 0; i--) { | |
B[C[A[i]] - 1] = A[i]; | |
C[A[i]]--; | |
} | |
} | |
int main() { | |
clock_t tic = clock(); | |
int* test = (int*)malloc(sizeof(int) * MAX); | |
int* result = (int*)malloc(sizeof(int) * MAX); | |
int i, e, j = 0; | |
for(i = 0; i < MAX; i++) { | |
e = scanf("%d", &test[i]); | |
if (e != EOF) j++; | |
} | |
countingsort(test, result, j); | |
/* | |
for(i = 0; i < j; i++) | |
printf("%d ", result[i]); | |
printf("\n"); | |
*/ | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment