-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcountSort.c
More file actions
49 lines (36 loc) · 914 Bytes
/
Copy pathcountSort.c
File metadata and controls
49 lines (36 loc) · 914 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <stdio.h>
#define MAX 100
void countSort(int arr[], int n) {
int output[100], count[MAX] = {0};
for (int i = 0; i < n; i++)
count[arr[i]]++;
for (int i = 1; i < MAX; i++)
count[i] += count[i - 1];
for (int i = n - 1; i >= 0; i--) {
output[count[arr[i]] - 1] = arr[i];
count[arr[i]]--;
}
for (int i = 0; i < n; i++)
arr[i] = output[i];
}
void printArray(int arr[], int n) {
printf("Array: ");
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
}
int main() {
int arr[100], n;
printf("Enter number of elements (0–99): ");
scanf("%d", &n);
for (int i = 0; i < n; i++) {
printf("Element %d: ", i + 1);
scanf("%d", &arr[i]);
}
printf("Original ");
printArray(arr, n);
countSort(arr, n);
printf("Sorted ");
printArray(arr, n);
return 0;
}