{"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\/\">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\/\">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\">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=\u201dbanner\u201d]\n<p>Below is C++ implementation.<\/p>\n[pastacode lang=\u201dcpp\u201d manual=\u201d%2F%2F%20C%2B%2B%20implementation%20of%20Comb%20Sort%0A%23include%3Cbits%2Fstdc%2B%2B.h%3E%0Ausing%20namespace%20std%3B%0A%20%0A%2F%2F%20To%20find%20gap%20between%20elements%0Aint%20getNextGap(int%20gap)%0A%7B%0A%20%20%20%20%2F%2F%20Shrink%20gap%20by%20Shrink%20factor%0A%20%20%20%20gap%20%3D%20(gap*10)%2F13%3B%0A%20%0A%20%20%20%20if%20(gap%20%3C%201)%0A%20%20%20%20%20%20%20%20return%201%3B%0A%20%20%20%20return%20gap%3B%0A%7D%0A%20%0A%2F%2F%20Function%20to%20sort%20a%5B0..n-1%5D%20using%20Comb%20Sort%0Avoid%20combSort(int%20a%5B%5D%2C%20int%20n)%0A%7B%0A%20%20%20%20%2F%2F%20Initialize%20gap%0A%20%20%20%20int%20gap%20%3D%20n%3B%0A%20%0A%20%20%20%20%2F%2F%20Initialize%20swapped%20as%20true%20to%20make%20sure%20that%0A%20%20%20%20%2F%2F%20loop%20runs%0A%20%20%20%20bool%20swapped%20%3D%20true%3B%0A%20%0A%20%20%20%20%2F%2F%20Keep%20running%20while%20gap%20is%20more%20than%201%20and%20last%0A%20%20%20%20%2F%2F%20iteration%20caused%20a%20swap%0A%20%20%20%20while%20(gap%20!%3D%201%20%7C%7C%20swapped%20%3D%3D%20true)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%2F%2F%20Find%20next%20gap%0A%20%20%20%20%20%20%20%20gap%20%3D%20getNextGap(gap)%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Initialize%20swapped%20as%20false%20so%20that%20we%20can%0A%20%20%20%20%20%20%20%20%2F%2F%20check%20if%20swap%20happened%20or%20not%0A%20%20%20%20%20%20%20%20swapped%20%3D%20false%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Compare%20all%20elements%20with%20current%20gap%0A%20%20%20%20%20%20%20%20for%20(int%20i%3D0%3B%20i%3Cn-gap%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20(a%5Bi%5D%20%3E%20a%5Bi%2Bgap%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20swap(a%5Bi%5D%2C%20a%5Bi%2Bgap%5D)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20swapped%20%3D%20true%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%7D%0A%20%0A%2F%2F%20Driver%20program%0Aint%20main()%0A%7B%0A%20%20%20%20int%20a%5B%5D%20%3D%20%7B8%2C%204%2C%201%2C%2056%2C%203%2C%20-44%2C%2023%2C%20-6%2C%2028%2C%200%7D%3B%0A%20%20%20%20int%20n%20%3D%20sizeof(a)%2Fsizeof(a%5B0%5D)%3B%0A%20%0A%20%20%20%20combSort(a%2C%20n)%3B%0A%20%0A%20%20%20%20printf(%22Sorted%20array%3A%20%5Cn%22)%3B%0A%20%20%20%20for%20(int%20i%3D0%3B%20i%3Cn%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%20printf(%22%25d%20%22%2C%20a%5Bi%5D)%3B%0A%20%0A%20%20%20%20return%200%3B%0A%7D%0A%0A\u201d message=\u201dc++\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><strong>JAVA<\/strong><\/p>\n[pastacode lang=\u201djava\u201d manual=\u201d%2F%2F%20Java%20program%20for%20implementation%20of%20Comb%20Sort%0Aclass%20CombSort%0A%7B%0A%20%20%20%20%2F%2F%20To%20find%20gap%20between%20elements%0A%20%20%20%20int%20getNextGap(int%20gap)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%2F%2F%20Shrink%20gap%20by%20Shrink%20factor%0A%20%20%20%20%20%20%20%20gap%20%3D%20(gap*10)%2F13%3B%0A%20%20%20%20%20%20%20%20if%20(gap%20%3C%201)%0A%20%20%20%20%20%20%20%20%20%20%20%20return%201%3B%0A%20%20%20%20%20%20%20%20return%20gap%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F%2F%20Function%20to%20sort%20arr%5B%5D%20using%20Comb%20Sort%0A%20%20%20%20void%20sort(int%20arr%5B%5D)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20int%20n%20%3D%20arr.length%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20initialize%20gap%0A%20%20%20%20%20%20%20%20int%20gap%20%3D%20n%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Initialize%20swapped%20as%20true%20to%20make%20sure%20that%0A%20%20%20%20%20%20%20%20%2F%2F%20loop%20runs%0A%20%20%20%20%20%20%20%20boolean%20swapped%20%3D%20true%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Keep%20running%20while%20gap%20is%20more%20than%201%20and%20last%0A%20%20%20%20%20%20%20%20%2F%2F%20iteration%20caused%20a%20swap%0A%20%20%20%20%20%20%20%20while%20(gap%20!%3D%201%20%7C%7C%20swapped%20%3D%3D%20true)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20Find%20next%20gap%0A%20%20%20%20%20%20%20%20%20%20%20%20gap%20%3D%20getNextGap(gap)%3B%0A%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20Initialize%20swapped%20as%20false%20so%20that%20we%20can%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20check%20if%20swap%20happened%20or%20not%0A%20%20%20%20%20%20%20%20%20%20%20%20swapped%20%3D%20false%3B%0A%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20Compare%20all%20elements%20with%20current%20gap%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20(int%20i%3D0%3B%20i%3Cn-gap%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if%20(arr%5Bi%5D%20%3E%20arr%5Bi%2Bgap%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20Swap%20arr%5Bi%5D%20and%20arr%5Bi%2Bgap%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20int%20temp%20%3D%20arr%5Bi%5D%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20arr%5Bi%5D%20%3D%20arr%5Bi%2Bgap%5D%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20arr%5Bi%2Bgap%5D%20%3D%20temp%3B%0A%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20Set%20swapped%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20swapped%20%3D%20true%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F%2F%20Driver%20method%0A%20%20%20%20public%20static%20void%20main(String%20args%5B%5D)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20CombSort%20ob%20%3D%20new%20CombSort()%3B%0A%20%20%20%20%20%20%20%20int%20arr%5B%5D%20%3D%20%7B8%2C%204%2C%201%2C%2056%2C%203%2C%20-44%2C%2023%2C%20-6%2C%2028%2C%200%7D%3B%0A%20%20%20%20%20%20%20%20ob.sort(arr)%3B%0A%20%0A%20%20%20%20%20%20%20%20System.out.println(%22sorted%20array%22)%3B%0A%20%20%20%20%20%20%20%20for%20(int%20i%3D0%3B%20i%3Carr.length%3B%20%2B%2Bi)%0A%20%20%20%20%20%20%20%20%20%20%20%20System.out.print(arr%5Bi%5D%20%2B%20%22%20%22)%3B%0A%20%0A%20%20%20%20%7D%0A%7D\u201d message=\u201djava\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\n<p><strong>PTHYON<\/strong><\/p>\n[pastacode lang=\u201dpython\u201d manual=\u201d%23%20Python%20program%20for%20implementation%20of%20CombSort%0A%20%0A%23%20To%20find%20next%20gap%20from%20current%0Adef%20getNextGap(gap)%3A%0A%20%0A%20%20%20%20%23%20Shrink%20gap%20by%20Shrink%20factor%0A%20%20%20%20gap%20%3D%20(gap%20*%2010)%2F13%0A%20%20%20%20if%20gap%20%3C%201%3A%0A%20%20%20%20%20%20%20%20return%201%0A%20%20%20%20return%20gap%0A%20%0A%23%20Function%20to%20sort%20arr%5B%5D%20using%20Comb%20Sort%0Adef%20combSort(arr)%3A%0A%20%20%20%20n%20%3D%20len(arr)%0A%20%0A%20%20%20%20%23%20Initialize%20gap%0A%20%20%20%20gap%20%3D%20n%0A%20%0A%20%20%20%20%23%20Initialize%20swapped%20as%20true%20to%20make%20sure%20that%0A%20%20%20%20%23%20loop%20runs%0A%20%20%20%20swapped%20%3D%20True%0A%20%0A%20%20%20%20%23%20Keep%20running%20while%20gap%20is%20more%20than%201%20and%20last%0A%20%20%20%20%23%20iteration%20caused%20a%20swap%0A%20%20%20%20while%20gap%20!%3D1%20or%20swapped%20%3D%3D%201%3A%0A%20%0A%20%20%20%20%20%20%20%20%23%20Find%20next%20gap%0A%20%20%20%20%20%20%20%20gap%20%3D%20getNextGap(gap)%0A%20%0A%20%20%20%20%20%20%20%20%23%20Initialize%20swapped%20as%20false%20so%20that%20we%20can%0A%20%20%20%20%20%20%20%20%23%20check%20if%20swap%20happened%20or%20not%0A%20%20%20%20%20%20%20%20swapped%20%3D%20False%0A%20%0A%20%20%20%20%20%20%20%20%23%20Compare%20all%20elements%20with%20current%20gap%0A%20%20%20%20%20%20%20%20for%20i%20in%20range(0%2C%20n-gap)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20arr%5Bi%5D%20%3E%20arr%5Bi%20%2B%20gap%5D%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20arr%5Bi%5D%2C%20arr%5Bi%20%2B%20gap%5D%3Darr%5Bi%20%2B%20gap%5D%2C%20arr%5Bi%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20swapped%20%3D%20True%0A%20%0A%20%0A%23%20Driver%20code%20to%20test%20above%0Aarr%20%3D%20%5B%208%2C%204%2C%201%2C%203%2C%20-44%2C%2023%2C%20-6%2C%2028%2C%200%5D%0AcombSort(arr)%0A%20%0Aprint%20(%22Sorted%20array%3A%22)%0Afor%20i%20in%20range(len(arr))%3A%0A%20%20%20%20print%20(arr%5Bi%5D)%2C%0A%20\u2033 message=\u201dpython\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\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>\u00a0<\/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 => 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 => 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 => 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 => 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 => 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=\u201dbanner\u201d]\n<p>\u00a0<\/p>\n<p>\u00a0<\/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}]}}