【问题标题】:Bubble Sort algorithm leaves array unchanged冒泡排序算法保持数组不变
【发布时间】:2021-02-23 07:14:56
【问题描述】:

我正在尝试编写一个简单的冒泡排序算法,但它不起作用。数组在最后打印时是不变的。

我在我的 IDE 上使用了调试工具,它告诉我第二个 for 循环没有增加,但我不知道为什么它没有增加。

我对学习 C++ 和一般算法还是很陌生,所以这方面的指针会很有帮助。

这是代码,非常感谢。

#include <iostream>

int main(){
    int A[] = {13, 89, 43, 74, 45, 16};
    int n = sizeof(A)/sizeof(*A);

    for (int i=0; i<n; i++) { //pass through the algorithm n-1 times
        int flag = 0;
        for (int j=0; j<n-i-1; j++) { //optimise checks, avoid checking sorted part of array
            if (A[j] > A[j+1]) {
                int temp = A[j];
                A[j] = A[j+1];
                A[j+1] = temp;
                flag = 1; //shows a swap happened
            }
            if (flag == 0) { //no swaps have occurred so the loop is over
                break;
            }
        }
    }
    for (int i = 0; i < n; i++) {
        std::cout << A[i] << " ";
    }
    return 0;
}

【问题讨论】:

    标签: c++ algorithm bubble-sort


    【解决方案1】:

    你的算法是错误的。您应该检查flag 第二个循环完成后

        int flag = 0;
        for (int j=0; j<n-i-1; j++) { //optimise checks, avoid checking sorted part of array
            if (A[j] > A[j+1]) {
                int temp = A[j];
                A[j] = A[j+1];
                A[j+1] = temp;
                flag = 1; //shows a swap happened
            }
        }
        if (flag == 0) { //no swaps have occurred so the loop is over
            break;
        }
    

    您在第二个循环之前初始化标志,但在第二个循环内检查它应该是一个线索,表明有些事情不太正确。所以应该让调试器告诉你第二个循环没有递增。

    有时,当您查看自己的代码时,您只会看到您认为自己编写的内容,而不是您实际编写的内容。客观地查看自己的代码是你需要训练自己去做的一种习惯。

    【讨论】:

      【解决方案2】:

      只有一个错误:

          if (flag == 0) { //no swaps have occurred so the loop is over
              break;
          }
      

      这应该在第二个for循环之外。

      #include <iostream>
      
      int main(){
          int A[] = {13, 89, 43, 74, 45, 16};
          int n = sizeof(A)/sizeof(*A);
      
          for (int i=0; i<n; i++) { //pass through the algorithm n-1 times
              int flag = 0;
              for (int j=0; j<n-i-1; j++) { //optimise checks, avoid checking sorted part of array
                  if (A[j] > A[j+1]) {
                      int temp = A[j];
                      A[j] = A[j+1];
                      A[j+1] = temp;
                      flag = 1; //shows a swap happened
                  }
              }
                  if (flag == 0) { //no swaps have occurred so the loop is over
                      break;  
              }
          }
          for (int i = 0; i < n; i++) {
              std::cout << A[i] << " ";
          }
          return 0;
      }
      

      【讨论】:

        【解决方案3】:

        最初检查A[j] &gt; A[j+1] 时,13 > 89 为假,flag 保持为 0 并执行 break 语句。这就是数组没有变化的原因。您应该在第二个循环完成后检查标志,如其他答案中所述。你听说过橡皮鸭调试吗?试一试。很有帮助。

        【讨论】:

          猜你喜欢
          • 2016-03-19
          • 1970-01-01
          • 2013-09-09
          • 1970-01-01
          • 2013-09-04
          • 2013-11-02
          • 2016-04-04
          • 1970-01-01
          • 2016-02-10
          相关资源
          最近更新 更多