{"id":434454,"date":"2018-04-24T09:38:14","date_gmt":"2018-04-24T09:38:14","guid":{"rendered":"https:\/\/essaypaper.org\/sorting-an-array-using-shell-sort-algorithm\/"},"modified":"2018-10-24T08:54:25","modified_gmt":"2018-10-24T08:54:25","slug":"sorting-an-array-using-shell-sort-algorithm","status":"publish","type":"post","link":"https:\/\/www.benedictsol.com\/blogs\/sorting-an-array-using-shell-sort-algorithm\/","title":{"rendered":"Sorting an Array Using Shell Sort Algorithm"},"content":{"rendered":"<div>\n<h2 style=\"text-align: center;\"><span style=\"font-weight: 400;\">Sorting an Array Using Shellsort<\/span><\/h2>\n<p><span style=\"font-weight: 400;\">In this guide, we will sort the array using Shell\u2019s algorithm \u2013 Shellsort. The algorithm works like this.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">The array is divided into groups, each of which consists of two elements. The distance between pairs of elements is d=n\/2, where n is the number of array elements. The elements of the pair are compared to each other and if needed, reversed. Then groups merge in pairs. Each new group has 4 elements, and distance between elements is d=d\/2. Sorting is performed in the middle of the group, and then the groups are merged. The process continues as long as the distance between items becomes 1. At this point, the array is sorted by the method of insertion. The number of comparisons is 0.5*n3\/2.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">The solution in C++ looks like this:<\/span><\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">&#13;\n&#13;\n#include&lt;iostream.h&gt;&#13;\n#include&lt;conio.h&gt;&#13;\n#include&lt;iomanip.h&gt;&#13;\nint v[20],                    &#13;\n     n;                       &#13;\n int m_comp=0, m_move=0&#13;\nvoid input() &#13;\n{&#13;\n        cout&lt;&lt;\"Enter number of elements (n&lt;20)\"&lt;&lt;endl; cin&gt;&gt;n;&#13;\n        cout&lt;&lt;\"Enter array\"&lt;&lt;endl;&#13;\n        for(int i=0;i&lt;n;i++) cin&gt;&gt;v[i];&#13;\n}&#13;\nvoid output()&#13;\n{&#13;\n   for(int i=0;i&lt;n;i++)                &#13;\n     cout&lt;&lt;setw(5)&lt;&lt;v[i]&lt;&lt;\" \";&#13;\n   cout&lt;&lt;endl; } void sort() { for(int k=n\/2;k&gt;0;k\/=2)              &#13;\n     for(int i=k;i&lt;n;i++) { m_comp++; for(int j=i-k;j&gt;=0&amp;&amp;v[j]&gt;v[j+k];j-=k)  &#13;\n               {       int temp=v[j];          &#13;\n                            v[j]=v[j+k];&#13;\n                                       v[j+k]=temp;&#13;\n                             m_move++;&#13;\n                            output();         &#13;\n                     }                        &#13;\n}&#13;\n}&#13;\n&#13;\nvoid main() &#13;\n{ &#13;\n        cout&lt;&lt;\"Sorting by Shell\"&lt;&lt;endl;                                                                                                     &#13;\n        input();&#13;\n        cout&lt;&lt;\"Array before sort\"&lt;&lt;endl; output();&#13;\n        sort();&#13;\n        cout&lt;&lt;\"Array after sort\"&lt;&lt;endl; output();    &#13;\ncout&lt;&lt;\"m_comp=\"&lt;&lt;m_comp&lt;&lt;\"  m_move=\"&lt;&lt;m_move&lt;&lt;endl; &#13;\ngetch();&#13;\n}&#13;\n&#13;\n<\/pre>\n<p><span style=\"font-weight: 400;\">And the work of the program can be illustrated using this table:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Original array: a_input= {20,-5, 8, 7, 0, 3, -1, 4};<\/span><\/p>\n<p>\u00a0<\/p>\n<table>\n<tbody>\n<tr>\n<td><span style=\"font-weight: 400;\">\u2116 of iteration<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Distance between the compared groups of elements <\/span><\/td>\n<td><span style=\"font-weight: 400;\">Groups of elements<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Result of the iteration<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">i=1<\/span><\/td>\n<td><span style=\"font-weight: 400;\">d=n\/2=8\/2=4<\/span><\/td>\n<td><span style=\"font-weight: 400;\">{{20,-5, 8, 7} {0, 3,-1, 4}}<\/span><\/td>\n<td><span style=\"font-weight: 400;\">{{0,-5,-1, 4} {20,3,8, 7}}<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">i=2<\/span><\/td>\n<td><span style=\"font-weight: 400;\">d=d\/2=4\/2=2<\/span><\/td>\n<td><span style=\"font-weight: 400;\">{{0,-5} {-1,4} {20,3} {8,7}}{{-1,-5} {0,4} {20,3} {8,7}}{{-1, -5} {0, 3} {20,4} {8,7}}<\/span><\/td>\n<td><span style=\"font-weight: 400;\">{{-1,-5}{0,3}{8,4}{20,7}}<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">i=3<\/span><\/td>\n<td><span style=\"font-weight: 400;\">d=d\/2=2\/2=1<\/span><\/td>\n<td><span style=\"font-weight: 400;\">{{-1} {-5} {0} {3} {8} {4} {20} {7}}{{-5} {-1} {0} {3} {4} {8} {7} {20}}<\/span><\/td>\n<td><span style=\"font-weight: 400;\">{-5,-1, 0, 3, 4, 7, 8, 20}<\/span><\/td>\n<\/tr>\n<tr>\n<td colspan=\"4\"><span style=\"font-weight: 400;\">End<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<blockquote>\n<p>\u00a0<\/p>\n<p><i><span style=\"font-weight: 400;\">In this guide, we have demonstrated how to sort an array using<\/span><\/i> <i>Shell sort algorithm <\/i><i><span style=\"font-weight: 400;\">\u2013 it should be read by students who have problems with similar topics. However, if you feel like you can\u2019t handle your homework, feel free to leave it to Assignment.EssayShark.com. Our expert will complete your assignment according to your requirements and academic standards. The completed assignment will be concise and correspond the task that was set. Our experts can deal with a variety of different assignments for different disciplines. The expert will choose an individual technique to solve your particular problem.<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\">The methods that our experts use depend on the discipline by which the work is done. Special recommendations and instructions provided by the student are strictly followed by our expert. Our expert can use practical, empirical, statistical, mathematical, diagnostic, and other methods to deal with your assignment. We are available 24\/7 so you can contact us any time you want. You should not struggle on your own \u2013 leave your assignment to us and we will deal with it successfully.<\/span><\/i><\/p>\n<p>\u00a0<\/p>\n<\/blockquote><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Sorting an Array Using Shellsort In this guide, we will sort the array using Shell\u2019s algorithm \u2013 Shellsort. The algorithm works like this. The array is divided into groups, each of which consists of two elements. The distance between pairs <a href=\"https:\/\/www.benedictsol.com\/blogs\/sorting-an-array-using-shell-sort-algorithm\/\" class=\"read-more\">Read More &#8230;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15,25],"tags":[],"class_list":["post-434454","post","type-post","status-publish","format-standard","hentry","category-essay-paper-writing","category-samples"],"_links":{"self":[{"href":"https:\/\/www.benedictsol.com\/blogs\/wp-json\/wp\/v2\/posts\/434454","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.benedictsol.com\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.benedictsol.com\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.benedictsol.com\/blogs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.benedictsol.com\/blogs\/wp-json\/wp\/v2\/comments?post=434454"}],"version-history":[{"count":0,"href":"https:\/\/www.benedictsol.com\/blogs\/wp-json\/wp\/v2\/posts\/434454\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.benedictsol.com\/blogs\/wp-json\/wp\/v2\/media?parent=434454"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.benedictsol.com\/blogs\/wp-json\/wp\/v2\/categories?post=434454"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.benedictsol.com\/blogs\/wp-json\/wp\/v2\/tags?post=434454"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}