counting-sort

first one is from geeksforgeeks.org but i feel much better with second one (mine)

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';
}
view raw count-sort.c hosted with ❤ by GitHub

Leave a comment