Skip to content

Instantly share code, notes, and snippets.

@zsrinivas
Created August 20, 2014 14:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zsrinivas/b75fb390d7d50d01c581 to your computer and use it in GitHub Desktop.
Save zsrinivas/b75fb390d7d50d01c581 to your computer and use it in GitHub Desktop.
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';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment