气泡排序算法-新秀

时间:2019-03-09 14:11:44

标签: sorting

所以我有这个问题。我正在尝试学习编码,为了做到这一点,我一直在尝试提出自己的做事方式,仅是为了练习。 关键是要有一个程序,询问每人吃10表时有多少煎饼,您输入数据,然后将其存储并按食用的煎饼数进行排序。 我已经看到气泡排序实现了“正确的方式”,我要问的是,我的逻辑有什么问题,错误在哪里?

void pancakes() {
`int i=1;
    int person[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    do {
        cout << "How many pancakes did person " << i << " eat ?" << endl;
        cin >> person[i-1];
        cout<<"For person number "<<i<<" you entered "<<person[i-1]<<" pancakes."<<endl;
        i++;

    } while (i!=11);
    i=10;
    while(i>=1){
        cout<<"Person number "<<i<<" had "<<person[i-1]<<" pancakes this morning."<<endl;
        i--;
    }
    cout<<"I can make a list of people sorted by the number of pancakes eaten."<<endl<<endl;
   int y;
   int x=0;
   bool swap = true;
   int j=2;
   while(1 < j) {
       if(swap==true) {
           swap = false;
            x=0;}

    while (10 >= x) {

          if (person[x] > person[x + 1]) {
                       y = person[x];
                       person[x] = person[x + 1];
                       person[x + 1] = y;
                       swap = true;
                       x++;
                   }
               }
           if(swap==false){
               j=0;
               int k=0;
               while(10 >= k){
                   cout<<"Place number "<<k+1<<" is given to the person who had "<<person[k]<<" pancakes for breakfast !"<<endl;
                   k++;
               }
           }

   }


}

}

0 个答案:

没有答案