Write A Program (WAP) in C to perform Shell Sorting on an Array Of Integers

Leave a Comment
/* The following C Program will first ask the number of elements in the array (100 is the MAX limit). Then the user will have to enter the specified number elements. Then the program will perform Shell Sorting & display the result. */


#include<stdio.h>
#include<stdlib.h>
#define MAX 100

void create(int a[MAX],int n)
{
int i;
for(i = 0; i <= n-1; i++)
{
printf("Enter %d Element In The List:",i+1);
scanf("%d",&a[i]);
}
}

void swap(int *ai,int *aj)
{
*ai = *ai + *aj;
*aj = *ai - *aj;
*ai = *ai - *aj;
}

void shell_sort(int a[MAX],int n)
{
int d = n/2,i,flag;

while(d > 0)
{
flag = 0;
for(i = 0; i <=n-d-1; i++)
{
if(a[i] > a[i+d])
{
swap(&a[i],&a[i+d]);
flag = 1;
}
}
if(flag == 0)
d = d/2;
}
}

void display(int a[MAX],int n)
{
int i;
for(i = 0; i <= n-1; i++)
{
printf("Data: %d\n",a[i]);
}
}

int main()
{
int a[MAX],n;
printf("Enter The Element In The List");
scanf("%d",&n);
create(a,n);
shell_sort(a,n);
display(a,n);
system("pause");
return 0;
}


OUTPUT
Write A Program (WAP) in C to perform Shell Sorting on an Array Of Integers
Write A Program (WAP) in C to perform Shell Sorting on an Array Of Integers

0 comments: