D
D
Den4_x2019-08-12 16:20:17
Java
Den4_x, 2019-08-12 16:20:17

How does the ShellSort algorithm work?

public static void shellSort(int[] array) {

        int step = array.length / 2;

        while (step != 0) {
            for (int i = 0; i < step; i++) {
//Как может работать сл-ий цикл, если тут j в последствии выходит за индексные рамки массива?
                for (int j = i + step; j < array.length; j += step) {

                    int temp = array[j];
                    int k;
//Тот же вопрос
                    for (k = j - step; k >= 0 && array[k] > temp; k -= step) {
                        array[k + step] = array[k];
                    }
                    array[k + step] = temp;
                }
            }
            step = step / 2;
        }

    }

The essence of the algorithm is clear, the same InsertionSort only here the interval was added. But with these 2 lines of code, I didn’t understand a bit (see above). What's the joke here?
-Thank you in advance for your reply.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Shitskov, 2019-08-12
@Zarom

How does it go beyond the index scope of the array if the loop condition is
?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question