Skip to main content

Simple Shell Sort Program using Functions in C

2 min read Updated June 30, 2026
Share:
On this page (6sections)

About this program

This is an example program in c sorting programs. Read the concept first: C Array, then study the code and output below.

Definition

Shellsort, also known as Shell sort or Shell’s method, is an in-place comparison sort. It can be seen as either a generalization of sorting by exchange or sorting by insertion. The method starts by sorting pairs of elements far apart from each other, then progressively reducing the gap between elements to be compared.

Simple Shell Sort C Program example using functions

/* Simple Shell Sort Program Using Functions and Array in C*/
/* Data Structure Programs,C Functions and Array Examples */

#include<stdio.h>
#include<conio.h>

#define MAX_SIZE 5

void shell_sort(int[]);

int main() {
  int arr_sort[MAX_SIZE], i;

  printf("Simple Shell Sort Example - Functions and Array\n");
  printf("\nEnter %d Elements for Sorting\n", MAX_SIZE);
  for (i = 0; i < MAX_SIZE; i++)
    scanf("%d", &arr_sort[i]);

  printf("\nYour Data   :");
  for (i = 0; i < MAX_SIZE; i++) {
    printf("\t%d", arr_sort[i]);
  }

  shell_sort(arr_sort);
  getch();

}

void shell_sort(int fn_arr[]) {
  int i, j, k, a, t;

  for (i = MAX_SIZE / 2; i > 0; i = i / 2) {
    for (j = i; j < MAX_SIZE; j++) {
      for (k = j - i; k >= 0; k = k - i) {
        if (fn_arr[k + i] >= fn_arr[k])
          break;
        else {
          //Swapping Values 
          t = fn_arr[k];
          fn_arr[k] = fn_arr[k + i];
          fn_arr[k + i] = t;
        }
      }

      printf("\nShell Sort Iteration [%d:%d] ", i, j);
      for (a = 0; a < MAX_SIZE; a++) {
        printf("\t%d", fn_arr[a]);
      }
    }

  }

  printf("\n\nSorted Data :");
  for (i = 0; i < MAX_SIZE; i++) {
    printf("\t%d", fn_arr[i]);
  }
}

Sample Output:

Simple Shell Sort Example - Functions and Array

Enter 5 Elements for Sorting
56
45
90
18
7

Your Data   :   56      45      90      18      7
Shell Sort Iteration [2:2]      56      45      90      18      7
Shell Sort Iteration [2:3]      56      18      90      45      7
Shell Sort Iteration [2:4]      7       18      56      45      90
Shell Sort Iteration [1:1]      7       18      56      45      90
Shell Sort Iteration [1:2]      7       18      56      45      90
Shell Sort Iteration [1:3]      7       18      45      56      90
Shell Sort Iteration [1:4]      7       18      45      56      90

Sorted Data :   7       18      45      56      90

------------------
(program exited with code: 0)

Press any key to continue . . .

Learn the concept first, then study the code:

Frequently Asked Questions

What does this C program do?
It is a C example program that demonstrates Simple Shell Sort Program using Functions, including the complete source code and the expected sample output.
How do I compile and run this C program?
Save the code in a `.c` file, compile it with `gcc filename.c -o program`, then run it with `./program` (or `program.exe` on Windows).
What concepts does this example use?
This example uses loops to iterate over data, arrays and conditional logic, illustrating a common pattern in C programming.

Related Tutorials

Search tutorials