C program of Shell Sort Algorithm | C code champ

C program of Shell Sort Algorithm | C code champ


C program of Shell Sort Algorithm : Shell sort is one of the oldest . Shell sort is a multi-pass algorithm which generalizes an exchanging sort, such as insertion or bubble sort, by starting the comparison and exchange of elements with elements that are far apart before finishing with neighboring elements. Starting with far apart elements can move some out-of-place elements into position faster than a simple nearest neighbor exchange.

Lets see how the sort a list of elements using shell sort :

  1. Arrange the data sequence in a two-dimensional array
  2. Sort the columns of the array

The effect of above two sequence is that, the data sequence is partially sorted. The process above is repeated, but each time with a narrower array, i.e. with a smaller number of columns. In the last step, the array consists of only one column. In each step, the sortedness of the sequence is increased, until in the last step it is completely sorted. However, the number of sorting operations necessary in each step is limited, due to the pre-sortedness of the sequence obtained in the preceding steps.

Lets us see how to write a which accepts number of elements and their values as input and displays the sorted list as output.

C program of Shell Sort Algorithm :


void shellsort(int a[],int n)
   int j,i,k,m,mid;
   for(m = n/2;m>0;m/=2)
         for(j = m;j< n;j++)
                mid = a[i];
                a[i] = a[i+m];
                a[i+m] = mid;

int main()
    int a[20],i,n;
    printf("----------------------Made by C code champ-----------------\n");
    printf("\t\tSHELL SORT ALGORITHM\n\n\n\n");
    printf("Enter total elements : ");
    scanf("%d", &n);
    printf("\nEnter %d Elements : ", n);
    for (i = 0; i < n; i++)
    scanf("%d", &a[i]);


    printf("\n\n\nSorted list by Shell sort :\n ");
    for(i=0;i< n;i++)
    return 0;

We hope that you all have enjoyed the of Shell Short Algorithm. If you have any queries or issues with the above code, ask us in form of comments.