Shell Sort
C Program For Shell Sort Source Code: #include<stdio.h> #include&l;conio.h> #include<math.h> void show(int A[20],int n) { int i; printf("\n"); for(i=1;i<=n;i++) { printf(" %d",A[i]); } } void shell_sort(int *A,int n) { int l,d,p,j,i,key; l=ceil(((log(((2*n)/3)+1)/log(2))/(log(3)/log(2)))-1); d=(pow(3,l)-1)/2; for(p=d;p>=1;p--) { for(j=d+1;j<=n;j++) { key=A[j]; i=j-d; while(i>0 && key<A[i]) { A[i+d]=A[i]; i=i-d; } A[i+d]=key; } d=(d-1)/3; } } void main() { int A[20],n,i; clrscr(); printf("\nEnter number of elements:"); scanf("%d",&n); for(i=1;i<=n;i++) { printf("\nEnter element %d :",i); scanf("%d",&A[i]); } shell_sort(A,n); printf("\nAfter sorting the array is:\n"); show(A,n); getch(); }
Tags:
Sorting
0 comments