From 502f8a8595e156792040c2be5f344cfece42ea8c Mon Sep 17 00:00:00 2001 From: Sumit Date: Wed, 6 Oct 2021 09:37:27 +0545 Subject: [PATCH] added shellsort algorithm --- ShellSort/README.md | 3 +++ ShellSort/shellsort.c | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 ShellSort/README.md create mode 100644 ShellSort/shellsort.c diff --git a/ShellSort/README.md b/ShellSort/README.md new file mode 100644 index 0000000..cd85ee0 --- /dev/null +++ b/ShellSort/README.md @@ -0,0 +1,3 @@ +## Shell Short + +Shellsort is an in-place comparison sort. It can either be seen as a generalization of sorting by exchange (bubble sort) or sorting by insertion (insertion sort). The method starts by sorting elements far apart from each other and progressively reducing the gap between them. Starting with far apart elements can move some out-of-place elements into position faster than a simple nearest neighbor exchange. Worst case time complexity is O(n2) and best case complexity is O(nlog(n)). diff --git a/ShellSort/shellsort.c b/ShellSort/shellsort.c new file mode 100644 index 0000000..4067f7b --- /dev/null +++ b/ShellSort/shellsort.c @@ -0,0 +1,43 @@ +/* + * C Program to sort an array using Shell Sort technique + */ +#include +void shellsort(int arr[], int num) +{ + int i, j, k, tmp; + for (i = num / 2; i > 0; i = i / 2) + { + for (j = i; j < num; j++) + { + for(k = j - i; k >= 0; k = k - i) + { + if (arr[k+i] >= arr[k]) + break; + else + { + tmp = arr[k]; + arr[k] = arr[k+i]; + arr[k+i] = tmp; + } + } + } + } +} +int main() +{ + int arr[30]; + int k, num; + printf("Enter total no. of elements : "); + scanf("%d", &num); + printf("\nEnter %d numbers: ", num); + + for (k = 0 ; k < num; k++) + { + scanf("%d", &arr[k]); + } + shellsort(arr, num); + printf("\n Sorted array is: "); + for (k = 0; k < num; k++) + printf("%d ", arr[k]); + return 0; +}