{"id":25200,"date":"2017-10-15T15:33:29","date_gmt":"2017-10-15T10:03:29","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=25200"},"modified":"2017-10-15T15:33:29","modified_gmt":"2017-10-15T10:03:29","slug":"shellsort","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/shellsort\/","title":{"rendered":"ShellSort"},"content":{"rendered":"<p>ShellSort is mainly a variation of <a href=\"http:\/\/quiz.geeksforgeeks.org\/insertion-sort\/\" target=\"_blank\" rel=\"noopener noreferrer\">Insertion Sort<\/a>.<span id=\"more-142314\"><\/span> In insertion sort, we move elements only one position ahead. When an element has to be moved far ahead, many movements are involved. The idea of shellSort is to allow exchange of far items. In shellSort, we make the array h-sorted for a large value of h. We keep reducing the value of h until it becomes 1. An array is said to be h-sorted if all sublists of every h\u2019th element is sorted.<\/p>\n<p>Following is C++ implementation of ShellSort.<\/p>\n<div class=\"responsive-tabs-wrapper\">\n<div class=\"responsive-tabs responsive-tabs--enabled\">\n<div id=\"tablist1-panel1\" class=\"tabcontent responsive-tabs__panel responsive-tabs__panel--active\">\u00a0<strong>C<\/strong><\/div>\n<div class=\"tabcontent responsive-tabs__panel responsive-tabs__panel--active\">\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">c<\/span> <\/div> <pre class=\"language-c code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-c code-embed-code\">\/\/ C++ implementation of Shell Sort<br\/>#include  &lt;iostream&gt;<br\/>using namespace std;<br\/> <br\/>\/* function to sort arr using shellSort *\/<br\/>int shellSort(int arr[], int n)<br\/>{<br\/>    \/\/ Start with a big gap, then reduce the gap<br\/>    for (int gap = n\/2; gap &gt; 0; gap \/= 2)<br\/>    {<br\/>        \/\/ Do a gapped insertion sort for this gap size.<br\/>        \/\/ The first gap elements a[0..gap-1] are already in gapped order<br\/>        \/\/ keep adding one more element until the entire array is<br\/>        \/\/ gap sorted <br\/>        for (int i = gap; i &lt; n; i += 1)<br\/>        {<br\/>            \/\/ add a[i] to the elements that have been gap sorted<br\/>            \/\/ save a[i] in temp and make a hole at position i<br\/>            int temp = arr[i];<br\/> <br\/>            \/\/ shift earlier gap-sorted elements up until the correct <br\/>            \/\/ location for a[i] is found<br\/>            int j;            <br\/>            for (j = i; j &gt;= gap &amp;&amp; arr[j - gap] &gt; temp; j -= gap)<br\/>                arr[j] = arr[j - gap];<br\/>             <br\/>            \/\/  put temp (the original a[i]) in its correct location<br\/>            arr[j] = temp;<br\/>        }<br\/>    }<br\/>    return 0;<br\/>}<br\/> <br\/>void printArray(int arr[], int n)<br\/>{<br\/>    for (int i=0; i&lt;n; i++)<br\/>        cout &lt;&lt; arr[i] &lt;&lt; &quot; &quot;;<br\/>}<br\/> <br\/>int main()<br\/>{<br\/>    int arr[] = {12, 34, 54, 2, 3}, i;<br\/>    int n = sizeof(arr)\/sizeof(arr[0]);<br\/> <br\/>    cout &lt;&lt; &quot;Array before sorting: \\n&quot;;<br\/>    printArray(arr, n);<br\/> <br\/>    shellSort(arr, n);<br\/> <br\/>    cout &lt;&lt; &quot;\\nArray after sorting: \\n&quot;;<br\/>    printArray(arr, n);<br\/> <br\/>    return 0;<br\/>}<\/code><\/pre> <\/div>\n<\/div>\n<div class=\"tabcontent responsive-tabs__panel responsive-tabs__panel--active\">\n[ad type=&#8221;banner&#8221;]\n<p><strong>JAVA<\/strong><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">java<\/span> <\/div> <pre class=\"language-java code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-java code-embed-code\">\/\/ Java implementation of ShellSort<br\/>class ShellSort<br\/>{<br\/>    \/* An utility function to print array of size n*\/<br\/>    static void printArray(int arr[])<br\/>    {<br\/>        int n = arr.length;<br\/>        for (int i=0; i&lt;n; ++i)<br\/>            System.out.print(arr[i] + &quot; &quot;);<br\/>        System.out.println();<br\/>    }<br\/> <br\/>    \/* function to sort arr using shellSort *\/<br\/>    int sort(int arr[])<br\/>    {<br\/>        int n = arr.length;<br\/> <br\/>        \/\/ Start with a big gap, then reduce the gap<br\/>        for (int gap = n\/2; gap &gt; 0; gap \/= 2)<br\/>        {<br\/>            \/\/ Do a gapped insertion sort for this gap size.<br\/>            \/\/ The first gap elements a[0..gap-1] are already<br\/>            \/\/ in gapped order keep adding one more element<br\/>            \/\/ until the entire array is gap sorted<br\/>            for (int i = gap; i &lt; n; i += 1)<br\/>            {<br\/>                \/\/ add a[i] to the elements that have been gap<br\/>                \/\/ sorted save a[i] in temp and make a hole at<br\/>                \/\/ position i<br\/>                int temp = arr[i];<br\/> <br\/>                \/\/ shift earlier gap-sorted elements up until<br\/>                \/\/ the correct location for a[i] is found<br\/>                int j;<br\/>                for (j = i; j &gt;= gap &amp;&amp; arr[j - gap] &gt; temp; j -= gap)<br\/>                    arr[j] = arr[j - gap];<br\/> <br\/>                \/\/ put temp (the original a[i]) in its correct<br\/>                \/\/ location<br\/>                arr[j] = temp;<br\/>            }<br\/>        }<br\/>        return 0;<br\/>    }<br\/> <br\/>    \/\/ Driver method<br\/>    public static void main(String args[])<br\/>    {<br\/>        int arr[] = {12, 34, 54, 2, 3};<br\/>        System.out.println(&quot;Array before sorting&quot;);<br\/>        printArray(arr);<br\/> <br\/>        ShellSort ob = new ShellSort();<br\/>        ob.sort(arr);<br\/> <br\/>        System.out.println(&quot;Array after sorting&quot;);<br\/>        printArray(arr);<br\/>    }<br\/>} <\/code><\/pre> <\/div>\n<p><strong>PYTHON<\/strong><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">python<\/span> <\/div> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\"># Python program for implementation of Shell Sort<br\/> <br\/>def shellSort(arr):<br\/> <br\/>    # Start with a big gap, then reduce the gap<br\/>    n = len(arr)<br\/>    gap = n\/2<br\/> <br\/>    # Do a gapped insertion sort for this gap size.<br\/>    # The first gap elements a[0..gap-1] are already in gapped <br\/>    # order keep adding one more element until the entire array<br\/>    # is gap sorted<br\/>    while gap &gt; 0:<br\/> <br\/>        for i in range(gap,n):<br\/> <br\/>            # add a[i] to the elements that have been gap sorted<br\/>            # save a[i] in temp and make a hole at position i<br\/>            temp = arr[i]<br\/> <br\/>            # shift earlier gap-sorted elements up until the correct<br\/>            # location for a[i] is found<br\/>            j = i<br\/>            while  j &gt;= gap and arr[j-gap] &gt;temp:<br\/>                arr[j] = arr[j-gap]<br\/>                j -= gap<br\/> <br\/>            # put temp (the original a[i]) in its correct location<br\/>            arr[j] = temp<br\/>        gap \/= 2<br\/> <br\/> <br\/># Driver code to test above<br\/>arr = [ 12, 34, 54, 2, 3]<br\/> <br\/>n = len(arr)<br\/>print (&quot;Array before sorting:&quot;)<br\/>for i in range(n):<br\/>    print(arr[i]),<br\/> <br\/>shellSort(arr)<br\/> <br\/>print (&quot;\\nArray after sorting:&quot;)<br\/>for i in range(n):<br\/>    print(arr[i]),<br\/> <\/code><\/pre> <\/div>\n<\/div>\n<\/div>\n<\/div>\n[ad type=&#8221;banner&#8221;]\n","protected":false},"excerpt":{"rendered":"<p>ShellSort &#8211; Searching and Sorting &#8211; ShellSort is mainly a variation of Insertion Sort. In insertion sort, we move elements only one position ahead. When an element has to be moved far ahead, many movements are involved. <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69969,71670,83505],"tags":[70054,2380,70478,71995,72054,70052,70263,70273,70268,71987,70461,71121,70978,71122,71129,70780,71993,70465,71151,70497,71868,70048,71144,70507,70271,70928,71216,70017,70979,71225,70969,34643,70262,71262,71263,71281,71267,70046,71145,71269,71266,71870,72056,71120,70016,70964,70965,71697,72055,71674,71125,71162,71695,70020,70019,70967,71156,72053,71990,72052],"class_list":["post-25200","post","type-post","status-publish","format-standard","hentry","category-algorithm","category-searching-and-sorting","category-shellsort","tag-algorithm","tag-array-sort","tag-avl-tree","tag-bein-sport","tag-best-sorting-algorithm-in-java","tag-binary-search","tag-binary-search-tree","tag-binary-sort","tag-binary-tree","tag-bitbucket","tag-bubble-sort","tag-bubble-sort-algorithm","tag-bubble-sort-c","tag-bubble-sort-java","tag-bubble-sort-program","tag-bubblesort","tag-bucket","tag-bucket-sort","tag-bucket-sort-algorithm","tag-counting-sort","tag-counting-sort-algorithm","tag-data-structures","tag-different-sorting-algorithms","tag-hash-table","tag-heap-sort","tag-heap-sort-algorithm","tag-insertion-sort","tag-insertion-sort-algorithm","tag-insertion-sort-c","tag-insertion-sort-program","tag-java-sorting-algorithms","tag-keyword","tag-merge-sort","tag-merge-sort-algorithm","tag-merge-sort-java","tag-merge-sort-program","tag-quick-sort-algorithm","tag-quicksort","tag-quicksort-c","tag-radix-sort","tag-radix-sort-algorithm","tag-radix-sort-algorithm-in-data-structure","tag-radix-sort-example-step-by-step","tag-selection-sort","tag-selection-sort-algorithm","tag-selection-sort-code","tag-selection-sort-program","tag-shell-sort","tag-shell-sort-algorithm","tag-sort","tag-sort-code","tag-sort-java","tag-sorted","tag-sorting-algorithms","tag-sorting-algorithms-in-java","tag-sorting-algorithms-java","tag-sorting-algorithms-with-examples","tag-sorting-in-java","tag-sorting-techniques-in-java","tag-types-of-sorting"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25200","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=25200"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25200\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=25200"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=25200"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=25200"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}