Skip to content

Commit fb91030

Browse files
authored
Merge pull request codeIIEST#30 from chirayujain98/count
counting sort algorithm
2 parents 7bf0cc4 + 00dfdb7 commit fb91030

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

sort/New Text Document.txt

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
38+
int main()
39+
{
40+
char arr[] = "chirayu";//"jain";
41+
42+
countSort(arr);
43+
44+
printf("Sorted character array is %sn", arr);
45+
return 0;
46+
}

0 commit comments

Comments
 (0)