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!

Recommended Answers

All 2 Replies

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;
        }
      }
    }
  }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.