Comb Sort AlgorithmIn this article, we will discuss the comb sort Algorithm. Comb Sort is the advanced form of bubble Sort. Bubble Sort compares all the adjacent values while comb sort removes all the turtle values or small values near the end of the list. It is a comparison-based sorting algorithm that is mainly an improvement in bubble sort. In bubble sort, there is a comparison between the adjacent elements to sort the given array. So, in bubble sort, the gap size between the elements that are compared is 1. Comb sort improves the bubble sort by using a gap of size more than 1. The gap in the comb sort starts with the larger value and then shrinks by a factor of 1.3. It means that after the completion of each phase, the gap is divided by the shrink factor 1.3. The iteration continues until the gap is 1. The shrink factor is found to be 1.3 by testing comb sort on 200,000 random lists. Comb sort works better than the bubble sort, but its time complexity in average case and worst case remains O(n2). The process of performing the comb sort is given as follows - STEP 1 START STEP 2 Calculate the gap value if gap value==1 goto step 5 else goto step 3 STEP 3 Iterate over data set and compare each item with gap item then goto step 4. STEP 4 Swap the element if require else goto step 2 STEP 5 Print the sorted array. STEP 6 STOP Now, let's see the algorithm of comb sort. AlgorithmWorking of comb Sort AlgorithmNow, let's see the working of the comb sort Algorithm. To understand the working of the comb sort algorithm, let's take an unsorted array. It will be easier to understand the comb sort via an example. Let the elements of array are - Now, initialize n = 8 // size of array gap = n shrink = 1.3 swapped = true First iterationgap = floor(gap/shrink) = floor(8/1.3) = 6 swapped = false This iteration ends here, because at i =2, the value of i + gap = 2 + 6 = 8, and there is no element at 8th position of the array. So, after first iteration, the elements of array will be - Now, move to iteration 2. Second iterationgap = floor(gap/shrink) = floor(6/1.3) = 4 swapped = false This iteration ends here, because at i =4, the value of i + gap = 4 + 4 = 8, and there is no element at 8th position of the array. So, after second iteration, the elements of array will be - Now, move to iteration 3. Third iterationgap = floor(gap/shrink) = floor(4/1.3) = 3 swapped = false This iteration ends here, because at i =5, the value of i + gap = 5 + 3 = 8, and there is no element at 8th position of the array. So, after third iteration, the elements of array will be - Now, move to iteration 4. Fourth iterationgap = floor(gap/shrink) = floor(3/1.3) = 2 swapped = false This iteration ends here, because at i =6, the value of i + gap = 6 + 2 = 8, and there is no element at 8th position of the array. So, after fourth iteration, the elements of array will be - Now, move to iteration 5. Fifth iterationgap = floor(gap/shrink) = floor(2/1.3) = 1 swapped = false After the fifth iteration, the sorted array is - Hence, the iterations end here, and now the sorting is completed. Now, the final sorted array is - Comb sort complexityNow, let's see the time complexity of Comb sort in the best case, average case, and worst case. We will also see the space complexity of Comb sort. 1. Time Complexity
2. Space Complexity
Implementation of Comb sortNow, let's see the programs of Comb sort in different programming languages. Program: Write a program to implement comb sort in C language. Output Program: Write a program to implement comb sort in C++. Output Program: Write a program to implement comb sort in C#. Output Program: Write a program to implement comb sort in Java. Output Program: Write a program to implement comb sort in PHP. Output So, that's all about the article. Hope the article will be helpful and informative to you. This article was not only limited to the algorithm. Along with the algorithm, we have also discussed the Comb Sort's complexity, working, and implementation in different programming languages.
Next TopicCounting Sort
|