C program that implements Shell sorting methods to sort a given list of integers in ascending order
C program that implements Shell sorting methods to sort a given list of integers in ascending order
Shell sorting methods
#include <stdio.h>
// Function to perform Shell Sort
void shellSort(int arr[], int n) {
// Start with a big gap, then reduce the gap
for (int gap = n / 2; gap > 0; gap /= 2) {
// Perform a gapped insertion sort
for (int i = gap; i < n; i++) {
int temp = arr[i];
int j;
// Shift earlier gap-sorted elements up
for (j = i; j >= gap && arr[j - gap] > temp; j -= gap) {
arr[j] = arr[j - gap];
}
// Put temp in its correct location
arr[j] = temp;
}
}
}
int main() {
int n,arr[20],i;
printf("Enter number of elements: ");
scanf("%d", &n);
printf("Enter %d integers:\n", n);
for (i = 0; i < n; i++)
scanf("%d", &arr[i]);
shellSort(arr, n);
printf("Sorted array: ");
for(i=0;i<n;i++)
printf(" %d",arr[i]);
return 0;
}
OUTPUT
Enter number of elements: 9
Enter 9 integers:
7 4 1 8 5 2 9 6 3
Sorted array: 1 2 3 4 5 6 7 8 9
π» Run This C Program
C program that implements Shell sorting methods to sort a given list of integers in ascending order
The selected C program is loaded automatically.
Edit the code, provide input through STDIN and
click Run.
The compiler is embedded using OneCompiler.
Your program runs in the embedded C environment;
Study Glance does not execute C code directly on
your web server.