-
Notifications
You must be signed in to change notification settings - Fork 2
/
Counting Sort.c
51 lines (43 loc) · 1.05 KB
/
Counting Sort.c
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
50
51
#include <stdio.h> // Include Standard C header file
#include <stdlib.h> // Include Stanard Libary file for some extra functions
int main()
{
int i,j,k,n,a[100],b[100],c[100]; // Create some arrays for later use
scanf("%d",&n);
for(i=0;i<n;i++){
printf("[i] Please Enter A Number :"); // Ask for a number
scanf("%d",&a[i]); // Add current number to the array
}
int min=a[0];
for(i=0;i<n;i++){
if(min>a[i])
min=a[i];
}
for(i=0;i<n;i++)
a[i]=a[i]-min+1;
int max=a[0];
for(i=0;i<n;i++){
if(max<a[i])
max=a[i];
}
int m=max;
for(i=0;i<=m;i++)
c[i]=0;
for(i=0;i<n;i++){
c[a[i]]=c[a[i]]+1;
}
for(i=1;i<=m;i++){
c[i]=c[i-1]+c[i];
}
for(i=0;i<n;i++){
b[c[a[i]]]=a[i];
c[a[i]]--;
}
printf ("Sorted Array is: \n");
for (int i=1; i<=n; i++)
{
b[i] = b[i]+min-1; // Sort the array in order
printf ("%d ", b[i]); // print in order
}
return 0; // Exit with no error code - exit with code zero
}