U
U
UID_B Nintendo2018-09-22 04:53:42
Java
UID_B Nintendo, 2018-09-22 04:53:42

What is the role of the first and second cycle in bubble sort?

//Вопрос в комментах помечен знаком (?)

    public void bubbleSort(){

        int in, out;
        for(out = nElements-1; out > 1; out--)// внешний цикл (обратный) это количество раз сравнений ?
            for(in = 0; in < out; in++)//внутренний цикл (прямой) это количество раз поменяется местами ?
                if(a[in] > a[in+1])// условия при котором меняются местами
                    swap(in, in+1);// меняться местами
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dimonchik, 2018-09-22
@kostyamega8

outer loop (reverse) is the number of times of comparisons ?

these are taking each element, you can’t take not everything and say that everything is sorted
and this is a comparison of the taken element with the rest

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question