川建军 发表于 2020-10-18 04:11:40

帮帮看哪里错了???

#include <iostream>
using namespace std;

void bubblesort(int arr[], int len)
{
    int temp;
    for(int i=0; i<len-1; i++)
    {
      for(int j=0; len-i-1; j++)
      {
            if(arr > arr)
            {
                temp = arr;
                arr = arr;
                arr = temp;
            }
      }
    }
}


int main(void)
{
    int arr = {6, -1, 4, 3, 8};
    bubblesort(arr, 5);
   
    for(int i=0; i<5; i++)
      cout << arr << ",";
    cout << endl;
    return 0;
}


川建军 发表于 2020-10-18 04:12:13

    225077 segmentation fault (core dumped)./a.out

没问题吧?????

风过无痕1989 发表于 2020-10-18 09:08:37

第9行少了j< ,加上程序就能从小到大排序了

小甲鱼的铁粉 发表于 2020-10-18 10:44:59

三楼正解
#include <iostream>
using namespace std;

void bubblesort(int arr[], int len)
{
    int temp;
    for(int i=0; i<len-1; i++)
    {
      for(int j=0; j<len-i-1; j++)
      {
            if(arr > arr)
            {
                temp = arr;
                arr = arr;
                arr = temp;
            }
      }
    }
}


int main(void)
{
    int arr = {6, -1, 4, 3, 8};
    bubblesort(arr, 5);
   
    for(int i=0; i<5; i++)
      cout << arr << ",";
    cout << endl;
    return 0;
}

川建军 发表于 2020-10-18 11:46:00

风过无痕1989 发表于 2020-10-18 09:08
第9行少了j< ,加上程序就能从小到大排序了

thank you!!!

风过无痕1989 发表于 2020-10-18 11:58:15

川建军 发表于 2020-10-18 11:46
thank you!!!

不用客气,给个最佳答案就行了
页: [1]
查看完整版本: 帮帮看哪里错了???