Java Program for Comb Sort Last Updated : 14 Dec, 2022 Comments Improve Suggest changes Like Article Like Report 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. 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 inversion counts with one swap and performs better than Bubble Sort.The shrink factor has been empirically found to be 1.3 (by testing Combsort on over 200, 000 random lists) [Source: Wiki]Although, it works better than Bubble Sort on average, worst case remains O(n2). Java // Java program for implementation of Comb Sort import java.io.*; public class CombSort { // To find gap between elements int getNextGap(int gap) { // Shrink gap by Shrink factor gap = (gap * 10) / 13; if (gap < 1) return 1; return gap; } // Function to sort arr[] using Comb Sort void sort(int arr[]) { int n = arr.length; // initialize gap int gap = n; // Initialize swapped as true to make sure that // loop runs boolean swapped = true; // Keep running while gap is more than 1 and last // iteration caused a swap while (gap != 1 || swapped == true) { // Find next gap gap = getNextGap(gap); // Initialize swapped as false so that we can // check if swap happened or not swapped = false; // Compare all elements with current gap for (int i = 0; i < n - gap; i++) { if (arr[i] > arr[i + gap]) { // Swap arr[i] and arr[i+gap] int temp = arr[i]; arr[i] = arr[i + gap]; arr[i + gap] = temp; // Set swapped swapped = true; } } } } // Driver method public static void main(String args[]) { CombSort ob = new CombSort(); int arr[] = { 8, 4, 1, 56, 3, -44, 23, -6, 28, 0 }; ob.sort(arr); System.out.println("sorted array"); for (int i = 0; i < arr.length; ++i) System.out.print(arr[i] + " "); } } /* This code is contributed by Rajat Mishra */ Output: sorted array -44 -6 0 1 3 4 8 23 28 56 Time Complexity: The worst-case complexity of this algorithm is O(n2).Auxiliary Space: O(1). Please refer complete article on Comb Sort for more details! Comment More infoAdvertise with us Next Article Java Program for Comb Sort K kartik Follow Improve Article Tags : Sorting Java Programs DSA Practice Tags : Sorting Similar Reads Java Program for Counting Sort Counting sort is a sorting technique based on keys between a specific range. It works by counting the number of objects having distinct key values (kind of hashing). Then doing some arithmetic to calculate the position of each object in the output sequence. Java // Java implementation of Counting So 2 min read Java Program for Bubble Sort Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. Bubble Sort in Java is not the best method to sort an array but is one of the most basic implementations for one to learn. In this article, we will learn how to write 2 min read Java Program for QuickSort Like Merge Sort, QuickSort is a Divide and Conquer algorithm. It picks an element as pivot and partitions the given array around the picked pivot. There are many different versions of QuickSort that pick pivot in different ways.Always pick first element as pivot.Always pick last element as pivot (im 2 min read Java Program for ShellSort 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'th element is sorted. Java // Java implementation of ShellSort class ShellSort { /* An utility function to print array of si 2 min read Java Program for Heap Sort Heap sort is a comparison-based sorting technique based on the Binary Heap data structure. It is similar to the selection sort where first find the maximum element and place it at the end. We repeat the same process for the remaining element. Heap Sort in JavaBelow is the implementation of Heap Sort 3 min read Like