C Program to sort the string, using shell sort technique. Shell sort is the one of the oldest sorting technique, quite well for all types of arrays in c. The shell sort is a “diminishing increment sort”, better known as a “comb sort” to the unwashed programming masses. The algorithm makes multiple passes through the list, and each time sorts a number of equally sized sets using the insertion sort . The size of the set to be sorted gets larger with each pass through the list, until the set consists of the entire list. This sets the insertion sort up for an almost-best case run each iteration with a complexity that approaches O(n) .
C program for String Sorting
#include <string.h> #include <stdio.h> #include <stdlib.h> void shell_sort(char *chars, int c) { register int i, j, space, k; char x, a[5]; a[0]=9; a[1]=5; a[2]=3; a[3]=2; a[4]=1; for(k=0; k < 5; k++) { space = a[k]; for(i=space; i < c; ++i) { x = chars[i]; for(j=i-space; (x < chars[j]) && (j >= 0); j=j-space) chars[j+space] = chars[j]; chars[j+space] = x; } } } int main() { char string[300]; printf("Enter a string:"); gets(string); shell_sort(string, strlen(string)); printf("The sorted string is: %s.n", string); return 0; }