{"id":25206,"date":"2017-10-15T15:38:05","date_gmt":"2017-10-15T10:08:05","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=25206"},"modified":"2017-10-15T15:38:05","modified_gmt":"2017-10-15T10:08:05","slug":"comb-sort","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/comb-sort\/","title":{"rendered":"Comb Sort"},"content":{"rendered":"<p>Comb Sort is mainly an improvement over Bubble Sort. Bubble sort always compares adjacent values. So all <a href=\"http:\/\/www.geeksforgeeks.org\/counting-inversions\/\" target=\"_blank\" rel=\"noopener\">inversions<\/a> are removed one by one. Comb Sort improves on Bubble Sort by using gap of size more than 1. The gap starts with a large value and shrinks by a factor of 1.3 in every iteration until it reaches the value 1. Thus Comb Sort removes more than one <a href=\"http:\/\/www.geeksforgeeks.org\/counting-inversions\/\" target=\"_blank\" rel=\"noopener\">inversion counts<\/a> with one swap and performs better than Bublle Sort.<\/p>\n<p>The shrink factor has been empirically found to be 1.3 (by testing Combsort on over 200,000 random lists) [Source: <a href=\"https:\/\/en.wikipedia.org\/wiki\/Comb_sort\" target=\"_blank\" rel=\"noopener\">Wiki<\/a>]\n<p>Although, it works better than Bubble Sort on average, worst case remains O(n<sup>2<\/sup>).<\/p>\n[ad type=&#8221;banner&#8221;]\n<p>Below is C++ implementation.<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">c++<\/span> <\/div> <pre class=\"language-cpp code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-cpp code-embed-code\">\/\/ C++ implementation of Comb Sort<br\/>#include&lt;bits\/stdc++.h&gt;<br\/>using namespace std;<br\/> <br\/>\/\/ To find gap between elements<br\/>int getNextGap(int gap)<br\/>{<br\/>    \/\/ Shrink gap by Shrink factor<br\/>    gap = (gap*10)\/13;<br\/> <br\/>    if (gap &lt; 1)<br\/>        return 1;<br\/>    return gap;<br\/>}<br\/> <br\/>\/\/ Function to sort a[0..n-1] using Comb Sort<br\/>void combSort(int a[], int n)<br\/>{<br\/>    \/\/ Initialize gap<br\/>    int gap = n;<br\/> <br\/>    \/\/ Initialize swapped as true to make sure that<br\/>    \/\/ loop runs<br\/>    bool swapped = true;<br\/> <br\/>    \/\/ Keep running while gap is more than 1 and last<br\/>    \/\/ iteration caused a swap<br\/>    while (gap != 1 || swapped == true)<br\/>    {<br\/>        \/\/ Find next gap<br\/>        gap = getNextGap(gap);<br\/> <br\/>        \/\/ Initialize swapped as false so that we can<br\/>        \/\/ check if swap happened or not<br\/>        swapped = false;<br\/> <br\/>        \/\/ Compare all elements with current gap<br\/>        for (int i=0; i&lt;n-gap; i++)<br\/>        {<br\/>            if (a[i] &gt; a[i+gap])<br\/>            {<br\/>                swap(a[i], a[i+gap]);<br\/>                swapped = true;<br\/>            }<br\/>        }<br\/>    }<br\/>}<br\/> <br\/>\/\/ Driver program<br\/>int main()<br\/>{<br\/>    int a[] = {8, 4, 1, 56, 3, -44, 23, -6, 28, 0};<br\/>    int n = sizeof(a)\/sizeof(a[0]);<br\/> <br\/>    combSort(a, n);<br\/> <br\/>    printf(&quot;Sorted array: \\n&quot;);<br\/>    for (int i=0; i&lt;n; i++)<br\/>        printf(&quot;%d &quot;, a[i]);<br\/> <br\/>    return 0;<br\/>}<\/code><\/pre> <\/div>\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 program for implementation of Comb Sort<br\/>class CombSort<br\/>{<br\/>    \/\/ To find gap between elements<br\/>    int getNextGap(int gap)<br\/>    {<br\/>        \/\/ Shrink gap by Shrink factor<br\/>        gap = (gap*10)\/13;<br\/>        if (gap &lt; 1)<br\/>            return 1;<br\/>        return gap;<br\/>    }<br\/> <br\/>    \/\/ Function to sort arr[] using Comb Sort<br\/>    void sort(int arr[])<br\/>    {<br\/>        int n = arr.length;<br\/> <br\/>        \/\/ initialize gap<br\/>        int gap = n;<br\/> <br\/>        \/\/ Initialize swapped as true to make sure that<br\/>        \/\/ loop runs<br\/>        boolean swapped = true;<br\/> <br\/>        \/\/ Keep running while gap is more than 1 and last<br\/>        \/\/ iteration caused a swap<br\/>        while (gap != 1 || swapped == true)<br\/>        {<br\/>            \/\/ Find next gap<br\/>            gap = getNextGap(gap);<br\/> <br\/>            \/\/ Initialize swapped as false so that we can<br\/>            \/\/ check if swap happened or not<br\/>            swapped = false;<br\/> <br\/>            \/\/ Compare all elements with current gap<br\/>            for (int i=0; i&lt;n-gap; i++)<br\/>            {<br\/>                if (arr[i] &gt; arr[i+gap])<br\/>                {<br\/>                    \/\/ Swap arr[i] and arr[i+gap]<br\/>                    int temp = arr[i];<br\/>                    arr[i] = arr[i+gap];<br\/>                    arr[i+gap] = temp;<br\/> <br\/>                    \/\/ Set swapped<br\/>                    swapped = true;<br\/>                }<br\/>            }<br\/>        }<br\/>    }<br\/> <br\/>    \/\/ Driver method<br\/>    public static void main(String args[])<br\/>    {<br\/>        CombSort ob = new CombSort();<br\/>        int arr[] = {8, 4, 1, 56, 3, -44, 23, -6, 28, 0};<br\/>        ob.sort(arr);<br\/> <br\/>        System.out.println(&quot;sorted array&quot;);<br\/>        for (int i=0; i&lt;arr.length; ++i)<br\/>            System.out.print(arr[i] + &quot; &quot;);<br\/> <br\/>    }<br\/>}<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n<p><strong>PTHYON<\/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 CombSort<br\/> <br\/># To find next gap from current<br\/>def getNextGap(gap):<br\/> <br\/>    # Shrink gap by Shrink factor<br\/>    gap = (gap * 10)\/13<br\/>    if gap &lt; 1:<br\/>        return 1<br\/>    return gap<br\/> <br\/># Function to sort arr[] using Comb Sort<br\/>def combSort(arr):<br\/>    n = len(arr)<br\/> <br\/>    # Initialize gap<br\/>    gap = n<br\/> <br\/>    # Initialize swapped as true to make sure that<br\/>    # loop runs<br\/>    swapped = True<br\/> <br\/>    # Keep running while gap is more than 1 and last<br\/>    # iteration caused a swap<br\/>    while gap !=1 or swapped == 1:<br\/> <br\/>        # Find next gap<br\/>        gap = getNextGap(gap)<br\/> <br\/>        # Initialize swapped as false so that we can<br\/>        # check if swap happened or not<br\/>        swapped = False<br\/> <br\/>        # Compare all elements with current gap<br\/>        for i in range(0, n-gap):<br\/>            if arr[i] &gt; arr[i + gap]:<br\/>                arr[i], arr[i + gap]=arr[i + gap], arr[i]<br\/>                swapped = True<br\/> <br\/> <br\/># Driver code to test above<br\/>arr = [ 8, 4, 1, 3, -44, 23, -6, 28, 0]<br\/>combSort(arr)<br\/> <br\/>print (&quot;Sorted array:&quot;)<br\/>for i in range(len(arr)):<br\/>    print (arr[i]),<br\/> <\/code><\/pre> <\/div>\n<p>Output :<\/p>\n<pre>Sorted array: \r\n-44 -6 0 1 3 4 8 23 28 56 \r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><center><strong>Illustration:<\/strong><\/center><br \/>\nLet the array elements be<\/p>\n<pre>8, 4, 1, 56, 3, -44, 23, -6, 28, 0<\/pre>\n<p>Initially gap value = 10<br \/>\nAfter shrinking gap value =&gt; 10\/1.3 = <strong>7<\/strong>;<\/p>\n<pre><strong> 8<\/strong> 4 1 56 3 -44 23 <strong>-6<\/strong> 28 0\r\n-6 4 <strong>1<\/strong> 56 3 -44 23  8 28 <strong>0<\/strong>\r\n-6 4 0 56 3 -44 23  8 28 1\r\n<\/pre>\n<p>New gap value =&gt; 7\/1.3 = <strong>5<\/strong>;<\/p>\n<pre>-44 4 0 <strong>56<\/strong> 3 -6 23 8 <strong>28<\/strong> 1\r\n-44 4 0 28 <strong>3<\/strong> -6 23 8 <strong>56<\/strong> 1\r\n-44 4 0 28 1 -6 23 8 56 3\r\n<\/pre>\n<p>New gap value =&gt; 5\/1.3 = <strong>3<\/strong>;<\/p>\n<pre>-44 1  <strong>0<\/strong> 28 4 <strong>-6<\/strong> 23 8 56 3\r\n-44 1 -6 <strong>28<\/strong> 4  0 <strong>23<\/strong> 8 56 3\r\n-44 1 -6 23 4  0 <strong>28<\/strong> 8 56 <strong>3<\/strong>\r\n-44 1 -6 23 4  0  3 8 56 28\r\n<\/pre>\n<p>New gap value =&gt; 3\/1.3 = <strong>2<\/strong>;<\/p>\n<pre>-44 1 -6 0 <strong>4<\/strong> 23 <strong>3<\/strong> 8 56 28\r\n-44 1 -6 0 3 <strong>23<\/strong> 4 <strong>8<\/strong> 56 28\r\n-44 1 -6 0 3 8 4 23 56 28\r\n<\/pre>\n<p>New gap value =&gt; 2\/1.3 = <strong>1<\/strong>;<\/p>\n<pre>-44 -6 <strong>1 0<\/strong> 3 8 4 23 56 28\r\n-44 -6 0 1 3 <strong>8 4<\/strong> 23 56 28\r\n-44 -6 0 1 3 4 8 23 <strong>56 28<\/strong>\r\n-44 -6 0 1 3 4 8 23 28 56 \r\n\r\nno more swaps required (Array sorted)\r\n<\/pre>\n<p><strong>Time Complexity : <\/strong>Worst case complexity of this algorithm is O(n<sup>2<\/sup>) and the Best Case complexity is O(n).<\/p>\n<p><strong>Auxiliary Space : <\/strong>O(1).<\/p>\n[ad type=&#8221;banner&#8221;]\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Comb Sort &#8211; Searching and Sorting &#8211; Comb Sort is mainly an improvement over Bubble Sort. Bubble sort always compares adjacent values. So all inversions are removed one by one. Comb Sort improves on Bubble Sort by using gap of size more than 1.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69969,83506,71670],"tags":[70054,72103,72099,70052,70461,71135,71141,71130,70538,70780,70465,71151,71146,70905,70536,70497,72102,72104,71186,71181,70895,71138,72100,70271,70877,71216,70888,70262,71262,71289,71276,71165,71280,71288,71273,71270,71282,71281,71264,71283,72097,70046,71717,71722,71269,71120,71697,72055,70020,71161,70534,72101,71219,70560,70879,70882,70532,72098],"class_list":["post-25206","post","type-post","status-publish","format-standard","hentry","category-algorithm","category-comb-sort","category-searching-and-sorting","tag-algorithm","tag-algorithm-for-bubble-sort-in-data-structure","tag-array-in-vba","tag-binary-search","tag-bubble-sort","tag-bubble-sort-algorithm-in-data-structure","tag-bubble-sort-animation","tag-bubble-sort-in-data-structure","tag-bubble-sort-time-complexity","tag-bubblesort","tag-bucket-sort","tag-bucket-sort-algorithm","tag-comparison-of-sorting-algorithms","tag-complexity-of-merge-sort","tag-complexity-of-sorting-algorithms","tag-counting-sort","tag-counting-sort-example","tag-define-comb","tag-difference-between-bubble-sort-and-insertion-sort","tag-difference-between-bubble-sort-and-selection-sort","tag-fastest-sorting-algorithm","tag-flowchart-for-bubble-sort","tag-function-in-vba","tag-heap-sort","tag-heap-sort-time-complexity","tag-insertion-sort","tag-insertion-sort-time-complexity","tag-merge-sort","tag-merge-sort-algorithm","tag-merge-sort-algorithm-in-c","tag-merge-sort-algorithm-with-example","tag-merge-sort-c","tag-merge-sort-c-code","tag-merge-sort-c-program","tag-merge-sort-code","tag-merge-sort-example","tag-merge-sort-explained","tag-merge-sort-program","tag-merge-sort-program-in-c","tag-program-for-merge-sort","tag-pseudocode","tag-quicksort","tag-quicksort-animation","tag-quicksort-explanation-with-example","tag-radix-sort","tag-selection-sort","tag-shell-sort","tag-shell-sort-algorithm","tag-sorting-algorithms","tag-sorting-algorithms-comparison","tag-sorting-algorithms-complexity","tag-sorting-algorithms-pdf","tag-sorting-in-c","tag-space-complexity-of-merge-sort","tag-time-complexity-of-heap-sort","tag-time-complexity-of-insertion-sort","tag-time-complexity-of-sorting-algorithms","tag-vba-array"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25206","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=25206"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25206\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=25206"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=25206"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=25206"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}