这个我的冒泡排序计划中的错误是什么?

时间:2012-09-03 11:23:21

标签: c++ sorting bubble-sort

我已经反复检查代码中的任何问题,但无法弄清楚为什么我的气泡分拣程序没有给出正确的输出。你能帮我识别吗?

#include <iostream.h>
#include <conio.h>

 using namespace std;

 main()

{
  int number[10];
  int temp=0;
  int i=0;

  cout<<"Please enter any ten numbers to sort one by one: "<<"\n";

  for (i=0;i<10;i++)
  {
      cin>>number[i];
      }
  i=0;

  for (i=0;i<10;i++)
  {

      if(number[i]>number[i+1])

      {
      temp=number[i+1];
      number[i+1]=number[i];
      number[i]=temp;
                          }

      }
      i=0;
      cout<<"The sorted numbers are given below:"<<"\n";

      for (i=0;i<10;i++)
      {
        cout<<number[i]<<"\n";  
          }

          getch();
  }

编辑: 我已经接受了你们所说的必须有一个外环。但我又在想我写的东西。我认为具有气泡条件的ONLY循环应该进行排序。这就是我的想法:

for (i=0;i<10;i++)
    if(number[i]>number[i+1])
    {
        temp=number[i+1];
        number[i+1]=number[i];
        number[i]=temp;

    }
} 

现在我解释一下我在想这个循环“应该”做什么。它首先将数字[0]与数字[1]进行比较。如果条件满足,它将执行IF语句的正文。然后我将增加1(i ++)。然后在下一次迭代中,比较的值将是数字[1]和数字[2]。那么为什么它不会发生并且循环仅在通过后退出?换句话说,可能是我试图要求IF语句不会在for循环中重复?在我看来它确实如此。我非常感谢你的帮助和观点,我的问题可能很小,但这就是我的进步。谢谢。

6 个答案:

答案 0 :(得分:3)

这只是冒泡排序的第一步。您需要重复排序部分,直到通过期间不执行排序。

答案 1 :(得分:2)

  • 您只运行一次气泡循环 - 您应该运行它直到所有内容都已排序
  • 您的气泡循环转到十,访问number[i+1],这是一种未定义的行为

以下是修复主循环的方法:

bool again;
do {
    again = false;
    for (i=0;i<9;i++)
    { //       ^-- Nine, not ten
        if(number[i]>number[i+1])
        {
            temp=number[i+1];
            number[i+1]=number[i];
            number[i]=temp;
            again = true;
        }
    }
} while (again);

答案 2 :(得分:1)

冒泡排序是O(n * n)(最坏情况)----&gt;需要一个从迭代开始到结束的内循环。那么你应该检查一下是否在中途完成以获得更好的情况,如O(nlogn)或O(n)。

for (i=0;i<9;i++)     //from 0 up to N-1
{ 
  failed=false;      //for the checking if finished
  for(int j=i+1;j<10) //from next element up to N
   {
     if(number[i]>number[j])
     {
         temp=number[j];
         number[j]=number[i];
         number[i]=temp;
         failed=true;
      }


   }
  if(!failed)break;         //if failed=false then it is done.
}

您的错误仅从0 ---> N

迭代一次
  • 外环应该从0 ---> N-1开始,因为下一个元素最后会是N.
  • 内环应该从i + 1变为N. i + 1表示next元素。

如果你需要在n * n iteratitons之前完成中断,你需要用布尔值通知

示例:

Initial : 4 9 3 6 2 5 7

1st pass: 4 3 6 2 5 7 9  ---> failed at many i(you see "9" bubbled )

2nd pass: 3 4 2 5 6 7 9 ---> failed at several i's(you see "6" bubbled)

3rd pass: 3 2 4 5 6 7 9 ---> failed(once) at 4 and 2(you see "4" bubbled)

4th pass: 2 3 4 5 6 7 9 ---> failed once at 2 and 3(3 bubbles)

5th pass: 2 3 4 5 6 7 9 --->same! did not fail. Finished without looping 2  more

答案 3 :(得分:1)

非常危险

int number[5];

并要求用户输入10个值; - )

答案 4 :(得分:1)

你超出了数组的范围。使循环运行5次,而不是10次。

其次,您尝试在一次传递中对数组进行排序。 制作嵌套循环。完整的排序。 例如,如果你有这个数组。

3  4  1  2  6

一次过后,正如你所做的那样,看起来就像这样。

3  1  2  4  6

所以这个数组没有完全排序。制作两个循环以完成排序。

确保运行循环直到size - 1

答案 5 :(得分:0)

可能会发生这种情况,因为您声明了一个大小为5 number[5]的数组但尝试输入并访问10个元素。

如果要在数组中包含10个元素,请将其声明为int number[10];

并使用for循环:

for (i=0;i<9;i++)     //from 0 up to N-1
{
  for(int j=i+1;j<10) //from next element up to N
   {
  if(number[i]>number[i+1])

     {
  temp=number[i+1];
  number[i+1]=number[i];
  number[i]=temp;
      }

   }
}