哪一个是气泡排序,还是两者都是? [英] Which of these is bubble sort or they are both?

查看:80
本文介绍了哪一个是气泡排序,还是两者都是?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这两种算法按升序对日期进行排序.这两种排序算法称为气泡排序吗?

1)首先,它通过使用swap在整个数组中找到最小的数组,并将其放在索引0中,依此类推.

或者换句话说->每次迭代一次后,它将通过使用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)每次迭代一次后,它将通过使用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--; 
}

它们都被称为气泡排序?

解决方案

在两种情况下,它都遵循bubbleort,但在第二种情况下,冒泡排序通过删除数组末尾的最大元素而变得高效./p>

现在,当数组具有长排序的最大元素尾部时,如果仍然有k,k-1,...,1作为前k个元素,而k + 1是之后依次为1亿.标准的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; 
    }

These two algorithms sort date in ascending order. Are these 2 sort algorithm are called bubble sort?

1) First it finds the smallest from all entire array by using swap and put it in the index 0, and so on.

Or other words --> After one each iteration it pushes the smallest value at the beginning of array by 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 one each iteration it pushes the biggest value at the end of array by 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--; 
}

They are both called bubble sort?

解决方案

In both the cases it is obeying the bubblesort, but in the second case the bubble sort made efficient by removing the largest element at the end of the array.

Now, that would still do a lot of unnecessary iterations when the array has a long sorted tail of largest elements, say you have k,k-1,...,1 as the first k elements and k+1 to 100000000 in order after that. The standard Bubble sort will pass k times through (almost) the entire array.

But you can try this in that matter:

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; 
    }

这篇关于哪一个是气泡排序,还是两者都是?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆