Why cant you have one loop for bubble sort?

假装没事ソ 提交于 2019-12-24 13:45:05

问题


I was just in an argument with my instructor about bubble sort, he told me that bubble sort is known as two for loops, one nested in the other. Which was not given before I started the assignment, so okay that is fine, but what is wrong with this code for a bubble sort:

int num = 0, i = 0;
bool go = true;
while (i < size - 1){
    if (array[i] > array[i + 1]){
        num = array[i];
        array[i] = array[i + 1];
        array[i + 1] = num;
        go = false;
    }
    i++;
    if (i >= size - 1 && go == false){
        i = 0;
        go = true;
    }
}

for (int i = 0; i < size; i++){
    cout << array[i];
}

does it not do the same thing as a bubble sort?

int i, j;
bool flag = true;
int temp;
int numLength = size;
for (i = 1; (i <= numLength) && flag; i++)
{
    flag = false;
    for (j = 0; j < (numLength - 1); j++)
    {
        if (array[j + 1] < array[j])
        {
            temp = array[j];
            array[j] = array[j + 1];
            array[j + 1] = temp;
            flag = true;
        }
    }
}

for (int i = 0; i < size; i++){
    cout << array[i];
}

return;

Thanks!


回答1:


The bubble sort algorithm needs two loops: the inner one iterating through the items and swapping them if adjacent ones are out of order, the outer one repeating until no more changes are made.

Your implementation does effectively have two loops. It's just that one of them is implemented using a flag and if condition, which resets the outer loop variable. It will do the same thing - loop through the items until no more need swapping.

Note, however, that constructing the algorithm in this way does not make it more efficient, or faster, or anything like that. It just makes it harder to figure out what is going on.




回答2:


You need nested loops because one pass through the array will not always sort all the elements.

Your code only simulates nested loops by resetting i when we've reached the end and still have things left to sort. Theoretically speaking, your code will have the same runtime as a nested bubble sort if given the same input array.

As to the question of whether you can; of course you can. But it's important to realize there is no benefit, in practice or in theory, to choosing one form over the other, at least as far as I can tell.

Also, when computing the time complexity of both algorithms, you will come to the conclusion that your algorithm, just like the form with nested loops, will need to perform the operation at most n times; the operation being a pass through the array, which is on the order of n. You will have to convince yourself that this is the case with your algorithm.

So no matter how you slice it (array pun intended, I guess?), bubble sort will have complexity O(n^2).




回答3:


Syntactically, you can have a one-loop bubble sort. Conceptually, you'll still have two loops.

does it not do the same thing as a bubble sort?

Yes, it does the same thing. Up to and including having a nested loop.

Your argument seems to be that if you only have one for loop in your code, you've created a one-loop bubble sort. It's a ruse though; there are still two loops happening algorithmically. And that's really the only thing that matters.




回答4:


Your code implements bubble sort. I can't see any advantages in your instructors code. Only arguments for two arguments is that it is bad style to change loop variable inside loop and we should avoid it.

And i would change

if (i >= size - 1 && go == false)

to

if (i == size - 1 && go == false)

since the first version introduces the misconception that I can be greater than size (but it can't)



来源:https://stackoverflow.com/questions/31665022/why-cant-you-have-one-loop-for-bubble-sort

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!