Hi,
I was trying to code the shell sort algorithm ,which compares data over distances of n/2,then n/4,n/8 and so on till the array finally gets sorted .As far I understood I implemented the code as follows but it doesn't seem to work.Can someone help!

here is the c code

void shellsort(int a[],int n)
{
     int i,x,temp;
     int inc=n/2;
     int lim;
     lim=n-inc;
     while(inc>0)
     {
                 lim=n-inc;
                 for(i=0;i<lim;i++)
                 {
                                   if(a[i]>a[i+inc])
                                   {
                                                    temp=a[i];
                                                    a[i]=a[i+inc];
                                                    a[i+inc]=temp;
                                                    }
                                                    }
                 inc=inc/2;
                 }
     printf("\nThe sorted array is:");
     for(i=0;i<n;i++)
     printf("%d  ",a[i]);
}

ouput
-----
Enter the number of elements:5
Enter the elements:5 4 3 2 1
The sorted order is:3 2 4 5 2368400

Please Help!

Edited 6 Years Ago by rohit_static: n/a

This is what I've used for Shell sort:

//Shell sort
void shellsort(int A[]) {
  int i, j, gap, temp;
  for(gap = MAX/2; gap > 0; gap /= 2) {
    for(i = gap; i < MAX; i++) {
      for(j = i - gap; j >= 0; j -= gap) {
        if(A[j] > A[j + gap]) {
	  temp = A[j];
	  A[j] = A[j + gap];
          A[j + gap] = temp;
        }
      }
    }
  }

Edited 6 Years Ago by Adak: n/a

This article has been dead for over six months. Start a new discussion instead.