File tree Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Original file line number Diff line number Diff line change
1
+ // C Program for counting sort
2
+ #include <stdio.h>
3
+ #include <string.h>
4
+ #define RANGE 255
5
+
6
+
7
+ void countSort(char arr[])
8
+ {
9
+
10
+ char output[strlen(arr)];
11
+
12
+ // Create a count array to store count of inidividul
13
+
14
+ int count[RANGE + 1], i;
15
+ memset(count, 0, sizeof(count));
16
+
17
+ for(i = 0; arr[i]; ++i)
18
+ ++count[arr[i]];
19
+
20
+ for (i = 1; i <= RANGE; ++i)
21
+ count[i] += count[i-1];
22
+
23
+ // Build the output character array
24
+ for (i = 0; arr[i]; ++i)
25
+ {
26
+ output[count[arr[i]]-1] = arr[i];
27
+ --count[arr[i]];
28
+ }
29
+
30
+ // Copy the output array to arr, so that arr now
31
+ // contains sorted characters
32
+ for (i = 0; arr[i]; ++i)
33
+ arr[i] = output[i];
34
+ }
35
+
36
+
37
+ int main()
38
+ {
39
+ char arr[] = "chirayu";//"jain";
40
+
41
+ countSort(arr);
42
+
43
+ printf("Sorted character array is %sn", arr);
44
+ return 0;
45
+ }
You can’t perform that action at this time.
0 commit comments