#include <stdio.h>
void CountingSort(int A[], int k) {
int B[10];
int max = A[0];
for (int i = 1; i < k; i++) {
if (A[i] > max)
max = A[i];
}
int C[10];
for (int i = 0; i <= max; ++i) {
C[i] = 0;
}
for (int i = 0; i < k; i++) {
C[A[i]]++;
}
for (int i = 1; i <= max; i++) {
C[i] += C[i - 1];
}
for (int i = k - 1; i >= 0; i--) {
B[C[A[i]] - 1] = A[i];
C[A[i]]--;
}
for (int i = 0; i < k; i++) {
A[i] = B[i];
}
}
void printA(int A[], int k) {
for (int i = 0; i < k; ++i) {
printf("%d ", A[i]);
}
printf("
");
}
int main() {
int A[] = {4, 2, 2, 8, 3, 3, 1};
int n = sizeof(A) / sizeof(A[0]);
CountingSort(A, n);
printA(A, n);
}
Code Example |
---|
C :: merge sort for strings in c |
C :: c int |
C :: text berjalan html |
C :: C Program to Find Largest and Smallest Number among N |
C :: extract substring after certain character in flutter |
C :: fgets function in c |
C :: addition.c |
C :: c fopen |
C :: malloc c include |
C :: number of hours, minutes, and seconds given the number of seconds. |
C :: bd number regex |
C :: getting string input in c |
C :: c program to find the frequency of characters in a string |
C :: check if pid exists c |
C :: c convert string to size_t |
C :: malloc |
C :: Installing Tailwind |
C :: c language float user input |
C :: signed and unsigned in c |
C :: C fscanf ignore commas |
C :: ltoa in c |
C :: snprintf c |
C :: %= in c |
C :: sphinx-doc |
C :: Symmetrical matrix in C |
C :: largest value in u64 |
C :: c program boilerplate |
C :: how to define pi in c |
C :: #define _TIMESPEC_DEFINED struct timespec { time_t tv_sec; long tv_nsec; }; |
C :: reading arrays from stdin c |