From 1990d26c9a45d75f334d3fac247f28a947a83376 Mon Sep 17 00:00:00 2001 From: Devashish Gupta Date: Mon, 8 Jul 2019 23:54:32 +0530 Subject: [PATCH] ShellSort Added (#34322) * ShellSort Added freeCodeCamp/guide/english/algorithms/sorting-algorithms/ shell-sort/index.md * Formatted Shell Sort Article * fix/formatting+grammar * fix/capitalization --- .../sorting-algorithms/shell-sort/index.md | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 guide/english/algorithms/sorting-algorithms/shell-sort/index.md diff --git a/guide/english/algorithms/sorting-algorithms/shell-sort/index.md b/guide/english/algorithms/sorting-algorithms/shell-sort/index.md new file mode 100644 index 0000000000..1a9bcd19d7 --- /dev/null +++ b/guide/english/algorithms/sorting-algorithms/shell-sort/index.md @@ -0,0 +1,27 @@ +--- +title: Shell Sort +--- +## Shell Sort + +Shell sort is mainly a variation of insertion sort. In insertion sort, you move elements only one position ahead. When an element has to be moved far ahead, many movements are involved. The idea of shell sort is to allow exchange of far items. In shell sort, you make the array h-sorted for a large value of h. You keep reducing the value of h until it becomes 1. An array is said to be h-sorted if all sublists of every h’th element is sorted. + +#### Java Implementation of Shell Sort (Method Only) + +```java +int sort(int arr[]) +{ + int n = arr.length; + for (int gap = n/2; gap > 0; gap /= 2) + { + for (int i = gap; i < n; i += 1) + { + int temp = arr[i]; + int j; + for (j = i; j >= gap && arr[ j - gap ] > temp; j -= gap) + arr[j] = arr[ j - gap ]; + arr[j] = temp; + } + } + return 0; +} +```