Shell Sort Example Program in C

C Program to Perform Shell Sort

This below program written in C is to perform the shell sort between the array elements. Gap insertion is the sequence by positive integers known as the increment sequence. This below code is the C example Program to perform Shell Sort and let you understand the shell sorting algorithm

#include <string.h>
#include <stdio.h>
#include <stdlib.h>

void shell(char *items, int count)
int i, j, gap, k;
char x, a[5];

a[0]=8; a[1]=5; a[2]=3; a[3]=2; a[4]=1;

for(k=0; k < 5; k++) {
gap = a[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]="asdfasdfasdfasdfasdf";

shell(s, strlen(s));
printf("The sorted string is: %s.\n", s);

return 0;

Related Topic A to Z Linux Commands quick reference sheet
How to Protect the Linux Machine against Dangerous Malware rootkits
Execute java program using shell script
Java code to Execute Shell script
Basic Linux Commands
C  algorithm 2011   Privacy Policy  Terms of Service  Feedback