first one is from geeksforgeeks.org but i feel much better with second one (mine)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void count_sort(char *str) | |
{ | |
int count[256];// all chars | |
char output[strlen(str)]; | |
memset(count, 0, sizeof(count)); | |
for (int i = 0; str[i]; i++) | |
count[str[i]]++; | |
for (int i = 1; i < 256; i++) | |
count[i]+=count[i-1]; | |
for (int i = 0; str[i]; i++) | |
{ | |
output[count[str[i]]-1] = str[i]; | |
count[str[i]]--; | |
} | |
for (int i = 0; str[i]; i++) { | |
str[i] = output[i]; | |
} | |
} | |
void counting_sort(char *str) | |
{ | |
int count[256]; | |
memset(count, 0, sizeof(count)); | |
for (int i = 0;str[i]; i++) | |
count[str[i]]++; | |
int j = 0; | |
for (int i = 0; i < 256; i++) | |
while(count[i]--) | |
str[j++] = i; | |
str[j]='\0'; | |
} |