Mega Code Archive

 
Categories / C / Code Snippets
 

The Shell Sort

#include <string.h> #include <stdio.h> #include <stdlib.h> void shell(char *items, int count) { int i, j, gap, k; char x, w[5]; w[0]=4; w[1]=3; w[2]=1; w[3]=8; w[4]=5; for(k=0; k < 5; k++) { gap = w[k]; for(i=gap; i < count; ++i) { x = items[i]; for(j=i-gap; (x < items[j]) && (j >= 0); j=j-gap){ items[j+gap] = items[j]; } items[j+gap] = x; } } } int main(void) { char s[255]="I Love Clementine"; shell(s, strlen(s)); printf("The sorted string is: %s.\n", s); return 0; }