-
Notifications
You must be signed in to change notification settings - Fork 29
/
counting sort
57 lines (38 loc) · 924 Bytes
/
counting sort
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
52
53
54
55
56
57
#include <stdio.h>
#include <string.h>
#define RANGE 255
void countSort(char arr[])
{
char output[strlen(arr)];
int count[RANGE + 1], i;
memset(count, 0, sizeof(count));
for(i = 0; arr[i]; ++i)
++count[arr[i]];
for (i = 1; i <= RANGE; ++i)
count[i] += count[i-1];
for (i = 0; arr[i]; ++i)
{
output[count[arr[i]]-1] = arr[i];
--count[arr[i]];
}
/*
For Stable algorithm
for (i = sizeof(arr)-1; i>=0; --i)
{
output[count[arr[i]]-1] = arr[i];
--count[arr[i]];
}
For Logic : See implementation
*/
for (i = 0; arr[i]; ++i)
arr[i] = output[i];
}
int main()
{
char arr[];
printf("enter the string");
scanf("%S",arr);
countSort(arr);
printf("Sorted character array is %sn", arr);
return 0;
}