Mega Code Archive

 
Categories / C / Code Snippets
 

The Bubble Sort

#include <string.h> #include <stdio.h> #include <stdlib.h> void bubble(char *items, int count); int main(void) { char s[255]; printf("Enter a string:"); gets(s); bubble(s, strlen(s)); printf("The sorted string is: %s.\n", s); return 0; } void bubble(char *items, int count) { register int k, j; register char t; for(k = 1; k < count; ++k) for( j = count-1; j >= k; --j) { if(items[j - 1] > items[ j ]) { /* exchange elements */ t = items[j - 1]; items[j - 1] = items[ j ]; items[ j ] = t; } } }