Shell sort in C.

 

01  #include <stdio.h>
02
03 void shellsort(int a[],int n)
04 {
05 int j,i,m,mid;
06 for(m = n/2;m>0;m/=2)
07 {
08 for(j = m;j< n;j++)
09 {
10 for(i=j-m;i>=0;i-=m)
11 {
12 if(a[i+m]>=a[i])
13 break;
14 else
15 {
16 mid = a[i];
17 a[i] = a[i+m];
18 a[i+m] = mid;
19 }
20 }
21 }
22 }
23 }
24
25 int main()
26 {
27 int a[10],i,n;
28 printf("Enter The number Of Elementst: ");
29 scanf("%d",&n);
30
31 for(i=0;i< n;i++)
32 {
33 printf("nElement %d : ",i+1);
34 scanf("%d",&a[i]);
35 }
36
37 printf("nArray Before Sorting : ");
38 for(i=0;i< n;i++)
39 printf("%d ",a[i]);
40 shellsort(a,n);
41
42 printf("nArray After Sorting : ");
43 for(i=0;i< n;i++)
44 printf("%d ",a[i]);
45
46 return 0;
47 }
 
 
OUTPUT :

Enter The number Of Elements : 5

Element 1 : 12

Element 2 : 32

Element 3 : 14

Element 4 : 22

Element 5 : 54

Array Before Sorting : 12 32 14 22 54

Array After Sorting : 12 14 22 32 54

Related Post

Leave a Reply

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.