shell sort inverse java

0

asked by anonymous 08.04.2017 / 23:23

1 answer

1

Good evening, to change from crescent to decreasing in your code, simply change the condition aux < v[j] to aux > v[j]

For example:

private static void shellsort(int v[], int n, boolean crescente) {
    int i, j, aux, h = 1;
    do
        h = 3 * h + 1;
    while (h < n);
    do {
        h /= 3;
        for (i = h; i < n; i++) {
            aux = v[i];
            j = i - h;
            if (crescente) {
                while (j >= 0 && aux < v[j]) {
                    v[j + h] = v[j];
                    j -= h;
                }
            } else {
                while (j >= 0 && aux > v[j]) {
                    v[j + h] = v[j];
                    j -= h;
                }
            }
            v[j + h] = aux;
        }
    } while (h > 1);
}
    
09.04.2017 / 02:53