The shellsort algorithm is described in detail in the Topic 4 lecture slides, as well as Wikipedia (https://en.wikipedia.org/wiki/Shellsort).
Write a function in C which implements the shell sort algorithm. The recommended approach is to create an array of sub arrays using dynamic memory allocation, sort those sub-arrays, and replace them in the array. This is achievable through the use of double pointers. The following may be helpful: https://www.geeksforgeeks.org/dynamically-allocate-2d-array-c/
Inputs:
Outputs:
NOTES:
Test Case 1:
Test Case 2:
#include <stdio.h>
void shell_sort(int a[], int n);
int main()
{
int a[15], n, i;
printf ("enter no. of elements to be inserted:");
scanf("%d", &n); //1. Enter value of n
for (i=0;i<n;i++)
{
printf ("\nenter the element:");
//2. Enter n elements
scanf ("%d", &a[i]);
}
shell_sort(a, n);
printf("\nthe sorted list is:");
for(i=0;i<n;i++)
printf("%d\t", a[i]);
return 0;
}
void shell_sort(int a[], int n)
{
int i,j, k, temp,count;
for (i=n/2;i>0;i=i/2)
{
for (j=i;j<n;j++)
{
for (k=j-i;k>=0;k--)
{
if(a[k+i]<a[k])
{
count++;
temp =a[k+i];
a[k+i]=a[k];
a[k]=temp;
}
}
}
}
printf ("Output is%d\n",count);
}
Get Answers For Free
Most questions answered within 1 hours.