Comb Sort | Java program for implementation of Comb Sort ; To find gap between elements ; Shrink gap by Shrink factor ; Function to sort arr [ ] using Comb Sort ; initialize gap ; Initialize swapped as true to make sure that loop runs ; Keep running while gap is more than 1 and last iteration caused a swap ; Find next gap ; Initialize swapped as false so that we can check if swap happened or not ; Compare all elements with current gap ; Swap arr [ i ] and arr [ i + gap ] ; Set swapped ; Driver method
class CombSort { int getNextGap ( int gap ) { gap = ( gap * 10 ) / 13 ; if ( gap < 1 ) return 1 ; return gap ; } void sort ( int arr [ ] ) { int n = arr . length ; int gap = n ; boolean swapped = true ; while ( gap != 1 swapped == true ) { gap = getNextGap ( gap ) ; swapped = false ; for ( int i = 0 ; i < n - gap ; i ++ ) { if ( arr [ i ] > arr [ i + gap ] ) { int temp = arr [ i ] ; arr [ i ] = arr [ i + gap ] ; arr [ i + gap ] = temp ; swapped = true ; } } } } 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 ] + " ▁ " ) ; } }