Shell Sort Programs in C


Levels of difficulty: / perform operation:

C Program to sort the string, using shell sort technique.

Program

   
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.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;
		}
	}
}
 
void main() 
{
	char string[300];
	clrscr();
	printf("Enter a string:");
	gets(string);
	shell_sort(string, strlen(string));
	printf("The sorted string is: %s.n", string);
	getch();
}


Output







Quantitative Aptitude
Reasoning
Programming
Interview