Skip to content

Commit

Permalink
shell_sort.c: Add Shell sort algorithm in C
Browse files Browse the repository at this point in the history
This implementation uses half of the array's size as the gap.
Then it keeps dividing it by 2.

Closes #135
  • Loading branch information
bihanviranga authored and sangamcse committed Oct 3, 2018
1 parent 59ab891 commit f0e3dbd
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) |


Expand Down
30 changes: 30 additions & 0 deletions shell_sort/C/shell_sort.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <stdio.h>

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;
}

0 comments on commit f0e3dbd

Please sign in to comment.