Skip to content

Commit 850a960

Browse files
committed
counting sort algorithm
1 parent ddf3334 commit 850a960

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

sort/New Text Document.txt

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
}

0 commit comments

Comments
 (0)