Mega Code Archive

 
Categories / C / Code Snippets
 

The Insertion Sort

#include <string.h> #include <stdio.h> #include <stdlib.h> void insert(char *items, int count) { register int j, b; char t; for(j=1; j < count; ++j) { t = items[j]; for(b=j-1; (b >= 0) && (t < items[b]); b--) items[b+1] = items[b]; // items[b+1] = t; items[b] = t; } } int main(void) { char s[255] = "I Love Clementine"; insert(s, strlen(s)); printf("The sorted string is: %s.\n", s); return 0; }