Which one is a bubble or both of them?

These two algorithms sort the date in ascending order. Are these two sorting algorithms called bubble sorting?

1) First, it finds the smallest of the entire array using swap and putting it at index 0, etc.

Or other words → After each iteration, it pops the smallest value at the beginning of the array using swap.

for (int i = 0; i != arrayEnd - 1; i++) {
    for (j = i + 1; j != arrayEnd; j++) {
        if (A[i] > A[j]) {
            temp = A[i];
            A[i] = A[j];
            A[j] = temp;
        }
    }
}

2) After each iteration, it pops the largest value at the end of the array using swap.

while (!isSorted) {
    isSorted = true;
    for (int i = 0; i < lastUnsorted; i++) {  // lastUnsorted = arrayLength - 1;
        if (A[i] > A[i + 1]) {
            temp = A[i];
            A[i] = A[i + 1];
            A[i + 1] = temp;
            isSorted = false;
        }
    }
    lastUnsorted--; 
}

Are they called bubble types?

+4
source share
3 answers

In both cases, it obeys the bubblesort, but in the second case, the sorting of the bubbles is effective by removing the largest element at the end of the array.

, , , k, k-1,..., 1 k k + 1 100000000 . Bubble k () .

:

while(!isSorted){
        isSorted = true;
        int lastSwap = lastUnsorted;
        for (int i = 0; i < lastSwap; i++) {  // lastUnsorted = arrayLength - 1;
            if (A[i] > A[i + 1]) {
                temp = A[i];
                A[i] = A[i + 1];
                A[i + 1] = temp;
                isSorted = false;
                currentSwap = j; 
            }
        }
        lastUnsorted--; 
        lastSwap = currentSwap; 
    }
+3

- . 1) - O (N ^ 2) 2) - , , .

+2

O (n ^ 2) Ω (n) . . Selection Sort O (n ^ 2) Ω (n ^ 2), .

, 1- , , .

My answer is no. They are not both types of bubbles. Only the 2nd type of bubble.

0
source

Source: https://habr.com/ru/post/1669468/


All Articles