diff --git a/README.md b/README.md index 642d2d7f..3ef986e2 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ This repository contains examples of various algorithms written on different pro | [Radix Sort](https://en.wikipedia.org/wiki/Radix_sort) | | | | [:octocat:](radix_sort/Python) | | [Binary Search](https://en.wikipedia.org/wiki/Binary_search_algorithm) | | [:octocat:](binary_search/Cpp) | | [:octocat:](binary_search/Python) | | [Bubble Sort](https://en.wikipedia.org/wiki/Bubble_sort) | [:octocat:](bubble_sort/C) | [:octocat:](bubble_sort/Cpp) | | | -| [Shell Sort](https://en.wikipedia.org/wiki/Shellsort) | | | | [:octocat:](shell_sort/Python) | +| [Shell Sort](https://en.wikipedia.org/wiki/Shellsort) | [:octocat:](shell_sort/C) | | | [:octocat:](shell_sort/Python) | | [Heap Sort](https://en.wikipedia.org/wiki/Heapsort) | | | | [:octocat:](heap_sort/python) | diff --git a/shell_sort/C/shell_sort.c b/shell_sort/C/shell_sort.c new file mode 100644 index 00000000..6bb74c97 --- /dev/null +++ b/shell_sort/C/shell_sort.c @@ -0,0 +1,30 @@ +#include + +void shellsort(int size, int *arr) { + // start with a big gap and reduce it + for (int gap = size/2; gap > 0; gap = gap/2) { + for (int i = gap; i < size; i = i+1) { + int temp = arr[i]; + + int j; + // shifting elements until the location for arr[i] is found + for (j = i; j >= gap && arr[j-gap] > temp; j = j-gap) { + arr[j] = arr[j-gap]; + } + arr[j] = temp; + } + } +} + +int main() { + int arr_size = 6; + int arr[6] = {10, 9, 8, 7, 6, 5 }; + shellsort(arr_size, arr); + + for (int i = 0; i < arr_size; i++) { + printf("%d ", arr[i]); + } + printf("\n"); + + return 0; +}