简体   繁体   English

C++ 冒泡排序算法

[英]C++ Bubble Sort Algorithm

I have the following code written in c++ and the algorithm works when in this scenario.我在 c++ 中编写了以下代码,并且该算法在这种情况下有效。 I am knew to c++ and don't understand what I did wrong in my 2nd test.我知道 c++ 并且不明白我在第二次测试中做错了什么。

#include <iostream>

using namespace std;

void bubbleSort(int numbers[], int size) {
    for (int i = 0; i<size;i++) {
        for (int j=0; j<size;j++) {
            if (numbers[j] > numbers[j+1]) {
                swap(numbers[j], numbers[j+1]);
            }
        }
    }
}

int main() {
    int numbers[] = {7,5,6,4};
    bubbleSort(numbers,4);
    
    for (int print = 0; print < 4; print++) {
        cout << numbers[print] << endl;
    }
    
    
    return 0;
}

But, fails when I try to put in numbers that are already sorted:但是,当我尝试输入已经排序的数字时失败了:

#include <iostream>

using namespace std;

void bubbleSort(int numbers[], int size) {
    for (int i = 0; i<size;i++) {
        for (int j=0; j<size;j++) {
            if (numbers[j] > numbers[j+1]) {
                swap(numbers[j], numbers[j+1]);
            }
        }
    }
}



int main() {
    int numbers[] = {1,2,3};
    bubbleSort(numbers,3);
    
    for (int print = 0; print < 3; print++) {
        cout << numbers[print] << endl;
    }
    
    
    return 0;
}
for (int j=0; j<size;j++) {

If size is 3, if the array has three values, for example, this loop iterates with values of j of 0, 1, and 2.例如,如果size为 3,如果数组具有三个值,则此循环将迭代j的值 0、1 和 2。

if (numbers[j] > numbers[j+1]) {

When j is 2 this compares numbers[2] with numbers[3] .j为 2 时,它会将numbers[2]numbers[3]进行比较。

There is no numbers[3] .没有numbers[3] This is undefined behavior.这是未定义的行为。 The loop is off by 1 value.循环关闭 1 个值。

Additionally, the overall bubble sort implementation is flawed.此外,整体的冒泡排序实现是有缺陷的。 In the shown code the inner loop iterates over the entire array (ignoring the off-by-1 bug), every time.在所示代码中,内部循环每次都遍历整个数组(忽略 off-by-1 错误)。 In a classical bubble sort the first pass (the first iteration of the outer loop) results in the inner loop iterating over the entire array and "bubbling" the smallest/largest value to the end of the array.在经典的冒泡排序中,第一次通过(外循环的第一次迭代)导致内循环遍历整个数组并将最小/最大值“冒泡”到数组的末尾。 On the next pass the inner loop does not need to iterate over the entire array, but only up until the 2nd smallest/largest position of the array.在下一次传递中,内部循环不需要迭代整个数组,而只需要迭代到数组的第二个最小/最大 position。 And so on, each pass (the outer loop) results in the inner loop iterating over a smaller, and smaller subset of the array, "bubbling" the corresponding value to the appropriate stop.依此类推,每次通过(外循环)都会导致内循环迭代数组的越来越小的子集,将相应的值“冒泡”到适当的停止点。

In addition to fixing the off-by-1 bug you'll also need to adjust the overall logic of this bubble sort, if you wish to get a perfect grade for your homework assignment.除了修复 off-by-1 错误之外,如果您希望获得完美的家庭作业成绩,您还需要调整此冒泡排序的整体逻辑。

Implementing bubble sort in its entirety is problematic.完全实施冒泡排序是有问题的。 In the example code, the inner loop repeatedly iterates over the full array while disregarding the shift by 1. The inner loop iterates over the whole array in a traditional bubble sort's first iteration of the outer loop, "bubbling" the smallest/largest value to the array's end.在示例代码中,内循环重复遍历整个数组,同时忽略移位 1。在传统冒泡排序的外循环的第一次迭代中,内循环遍历整个数组,将最小/最大值“冒泡”到阵列的末端。 On the subsequent iteration, the inner loop only has to iterate up to the array's second-smallest/largest point rather than the full array.在随后的迭代中,内部循环只需迭代到数组的第二小/最大点,而不是整个数组。 The inner loop then iterates through a smaller and smaller subset of the array, making bubbles of the associated the associated value to the proper stop with each successive run in the outside loop.然后,内部循环遍历数组的一个越来越小的子集,使关联值的气泡在外部循环中的每次连续运行中停止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM