HiveBrain v1.2.0
Get Started
← Back to all entries
snippetjavaMinor

Comb sort in Java

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
sortcombjava

Problem

Comb sort may be thought of as a generalization of bubble sort. The following is my implementation:

```
package net.coderodde.util.sorting;

import java.util.Arrays;
import java.util.Comparator;
import java.util.Random;

/**
* This class implements
* Comb sort.
*
* @author Rodion "rodde" Efremov
* @version 1.6 (Nov 29, 2015)
*/
public class CombSort {

private static final float SHRINK_FACTOR = 1.3f;

public static void sort(T[] array,
int fromIndex,
int toIndex,
Comparator comparator) {
int rangeLength = toIndex - fromIndex;

if (rangeLength = 1 && swapped) {
gap = Math.max(1, (int)(gap / SHRINK_FACTOR));
swapped = false;

for (int i = fromIndex; i + gap 0) {
T tmp = array[i];
array[i] = array[i + gap];
array[i + gap] = tmp;
swapped = true;
}
}
}
}

public static void sort(T[] array, Comparator comparator) {
sort(array, 0, array.length, comparator);
}

private static final int ARRAY_LENGTH = 1_000_000;
private static final int FROM_INDEX = 100;
private static final int TO_INDEX = ARRAY_LENGTH - 100;

public static void main(final String... args) {
long seed = System.nanoTime();
Random random = new Random(seed);
Integer[] array1 = createRandomIntegerArray(ARRAY_LENGTH, random);
Integer[] array2 = array1.clone();

System.out.println("Seed = " + seed);

long startTime = System.nanoTime();
CombSort.sort(array1, FROM_INDEX, TO_INDEX, Integer::compare);
long endTime = System.nanoTime();

System.out.printf("Comb sort in %.2f milliseconds.\n",
1.0 * (endTime - startTime) / 1e6);

startTime = System.nanoTime();
Arrays.sort(array2, FROM_INDEX, TO_I

Solution

I made an argument validation to the method. Changed the while loop to do_while loop. Swapped the condition check order in the do_while loop (I think it should be more efficient and a bit more readable that way).

public static  void sort(T[] array,
                            int fromIndex, 
                            int toIndex,
                            Comparator comparator) {
    if (fromIndex >= toIndex) {
        throw new IllegalArgumentException("fromIndex must be lower than toIndex");
    }

    int elementsToSort = toIndex - fromIndex;
    if (elementsToSort > 1) {
        sortImpl(array, fromIndex, toIndex, comparator);
    }
}

private static  void sortImpl(T[] array,
                            int fromIndex, 
                            int toIndex,
                            Comparator comparator) {
    int gap = toIndex - fromIndex;
    boolean swapped;

    do {
        gap = Math.max(1, (int)(gap / SHRINK_FACTOR));
        swapped = false;

        for (int i = fromIndex; i + gap  0) {
                T tmp = array[i];
                array[i] = array[i + gap];
                array[i + gap] = tmp;
                swapped = true;
            }
        }
    } while (swapped && gap >= 1);
}


Note: Consider adding a sort method that would not operate on the instance of the array:

public static  T[] sorted(T[] array,
                            int fromIndex, 
                            int toIndex,
                            Comparator comparator) {
    if (fromIndex >= toIndex) {
        throw new IllegalArgumentException("fromIndex must be lower than toIndex");
    }

    T[] result = array.clone();
    sort(result, fromIndex, toIndex, comparator);
    return result;
}

public static  T[] sorted(T[] array, Comparator comparator) {
    return sorted(array, 0, array.length, comparator);
}

Code Snippets

public static <T> void sort(T[] array,
                            int fromIndex, 
                            int toIndex,
                            Comparator<? super T> comparator) {
    if (fromIndex >= toIndex) {
        throw new IllegalArgumentException("fromIndex must be lower than toIndex");
    }

    int elementsToSort = toIndex - fromIndex;
    if (elementsToSort > 1) {
        sortImpl(array, fromIndex, toIndex, comparator);
    }
}

private static <T> void sortImpl(T[] array,
                            int fromIndex, 
                            int toIndex,
                            Comparator<? super T> comparator) {
    int gap = toIndex - fromIndex;
    boolean swapped;

    do {
        gap = Math.max(1, (int)(gap / SHRINK_FACTOR));
        swapped = false;

        for (int i = fromIndex; i + gap < toIndex; ++i) {
            if (comparator.compare(array[i], array[i + gap]) > 0) {
                T tmp = array[i];
                array[i] = array[i + gap];
                array[i + gap] = tmp;
                swapped = true;
            }
        }
    } while (swapped && gap >= 1);
}
public static <T> T[] sorted(T[] array,
                            int fromIndex, 
                            int toIndex,
                            Comparator<? super T> comparator) {
    if (fromIndex >= toIndex) {
        throw new IllegalArgumentException("fromIndex must be lower than toIndex");
    }

    T[] result = array.clone();
    sort(result, fromIndex, toIndex, comparator);
    return result;
}

public static <T> T[] sorted(T[] array, Comparator<? super T> comparator) {
    return sorted(array, 0, array.length, comparator);
}

Context

StackExchange Code Review Q#112213, answer score: 3

Revisions (0)

No revisions yet.