【问题标题】:Implementing Algorithms (Bubble Sort) to Organize an Array实施算法(冒泡排序)来组织数组
【发布时间】:2015-11-26 14:12:15
【问题描述】:

今天我正在练习编写一个程序,该程序基本上将用户的输入从最大值到最小值进行排序,并在排序后显示信息。为了做到这一点,我查阅了一些算法并遇到了冒泡排序方法。在阅读了一些文章之后,我了解了它的基本工作原理,因此我继续回顾了一些冒泡排序算法的示例编码,并将我从示例中学到的知识应用到了我的程序中。从我收到的输出来看,冒泡排序方法似乎在某种程度上有效。据我所知,我编写的冒泡排序算法/代码似乎只遍历数组一次,而不是遍历数组,直到所有整数都被排序到一个从最大值到最小值的列表中。有人可以帮我检查我的代码(尤其是冒泡排序部分)并指出正确的方向吗?

代码如下:

#include < iostream >

using namespace std;

int main() {
    int pancakes[10];
    int x;
    int valueSwitched;


    for (x = 0; x < 10; x++) {
        cout << "Please input the number of pancakes person " << (x + 1) << " has eaten.\n";
        cin >> pancakes[x];
    }

    for (int x = 0; x < 9; x++) {
        for (int y = 0; y < 9; y++) {
            if (pancakes[x] > pancakes[x + 1]) {
                int valueSwitched = pancakes[x];
                pancakes[x] = pancakes[x + 1];
                pancakes[x + 1] = valueSwitched;
            }
        }
    }

    cout << "\n-----ListSorted-----\n\n";

    for (int x = 0; x < 10; x++) {
        cout << "Person " << (x + 1) << " ate: " << pancakes[x] << endl;
    }

    return 0;
}

【问题讨论】:

  • 我不知道冒泡排序,但看起来你有一个循环 for (int y=0;y&lt;9;y++){y 没有在循环体内使用

标签: c++ arrays algorithm sorting bubble-sort


【解决方案1】:

您的排序代码与

执行相同的操作
for (int x = 0; x < 9; x++) {
    if (pancakes[x] > pancakes[x + 1]) {
        int valueSwitched = pancakes[x];
        pancakes[x] = pancakes[x + 1];
        pancakes[x + 1] = valueSwitched;
    }
}

耗时 9 倍。

您将不得不交换xy 的循环。

for (int y = 0; y < 9; y++)
{
    for (int x = 0; x < 9; x++)
    {
        if (pancakes[x] > pancakes[x + 1])
        {
            int valueSwitched = pancakes[x];
            pancakes[x] = pancakes[x + 1];
            pancakes[x + 1] = valueSwitched;
        }
    }
}

或者为了更有效地做到这一点,试试这个:

for (int y = 9; y > 0; y--)
{
    for (int x = 0; x < y; x++)
    {
        if (pancakes[x] > pancakes[x + 1])
        {
            int valueSwitched = pancakes[x];
            pancakes[x] = pancakes[x + 1];
            pancakes[x + 1] = valueSwitched;
        }
    }
}

【讨论】:

  • 没关系,可能回答了这个问题,但是阅读 OP 发布的程序,在我看来,你应该对一组对(人,煎饼)进行排序,以跟踪哪个人吃了给定的数字煎饼。
【解决方案2】:

我在sorting 的数字中发现了一个问题。所以你需要先学习bubble sort。我对您的代码进行了一些更改。如下所示:

for (int x = 0; x < 9; x++) {
        for (int y = 0; y < 9-x; y++) {// changes
            if (pancakes[y] > pancakes[y + 1]) {//changes
                int valueSwitched = pancakes[y];
                pancakes[y] = pancakes[y + 1];
                pancakes[y + 1] = valueSwitched;
            }
        }
    }

现在输入9,8,7,6,5,4,3,2,1,0,它就可以工作了..

【讨论】:

    【解决方案3】:

    在循环的内部循环中,您正在比较和交换 pancakes[x]pancakes[x + 1],而不是比较和交换 pancakes[y] 和 pancakes[y + 1]。

    for (int x = 0; x < 9; x++) {
        for (int y = 0; y < 9; y++) {
            if (pancakes[x] > pancakes[x + 1]) {
                int valueSwitched = pancakes[x];
                pancakes[x] = pancakes[x + 1];
                pancakes[x + 1] = valueSwitched;
            }
        }
    }
    

    我认为这只是一个错字。

    不过我还是要指出,使用魔法并不是一个好主意 像 9 这样的数字。还有使用名称 i、j、k、l、m 和 n 作为索引的传统。

    算法本身可以在数组已经排序后停止。

    考虑到您可以使用标准函数std::swap 来交换数组的元素。

    当数组已经排序后循环停止迭代时,我将展示这种方法

    #include <iostream>
    
    int main()
    {
        const size_t N = 10;
        int pancakes[N];
    
        for ( size_t i = 0; i < N; i++ ) 
        {
            std::cout << "Please input the number of pancakes person " << i + 1 << " has eaten: ";
            std::cin >> pancakes[i];
        }
    
        size_t n = N;
        while ( !( n < 2 ) )
        {
            size_t last_sorted = 0;
            for ( size_t i = 1; i < n; i++) 
            {
                if ( pancakes[i-1] > pancakes[i] ) 
                {
                    int valueSwitched = pancakes[i-1];
                    pancakes[i-1] = pancakes[i];
                    pancakes[i] = valueSwitched;
                    last_sorted = i;
                }
            }
    
            n = last_sorted;
        }
    
        std::cout << "\n-----ListSorted-----\n\n";
    
        for ( size_t i = 0; i < N; i++ ) 
        {
            std::cout << "Person " << i + 1 << " ate: " << pancakes[i] << std::endl;
        }
    
        return 0;
    }
    

    如果以输入为例

    10 1 9 2 8 3 7 4 6 5
    

    你会得到

    Please input the number of pancakes person 1 has eaten: 10
    Please input the number of pancakes person 2 has eaten: 1
    Please input the number of pancakes person 3 has eaten: 9
    Please input the number of pancakes person 4 has eaten: 2
    Please input the number of pancakes person 5 has eaten: 8
    Please input the number of pancakes person 6 has eaten: 3
    Please input the number of pancakes person 7 has eaten: 7
    Please input the number of pancakes person 8 has eaten: 4
    Please input the number of pancakes person 9 has eaten: 6
    Please input the number of pancakes person 10 has eaten: 5
    
    -----ListSorted-----
    
    Person 1 ate: 1
    Person 2 ate: 2
    Person 3 ate: 3
    Person 4 ate: 4
    Person 5 ate: 5
    Person 6 ate: 6
    Person 7 ate: 7
    Person 8 ate: 8
    Person 9 ate: 9
    Person 10 ate: 10
    

    如果你想保持一个人和这个人吃的煎饼之间的关系,那么你应该声明一个对数组。

    这是一个演示程序

    #include <iostream>
    #include <utility>
    
    int main()
    {
        const size_t N = 10;
        std::pair<size_t, int> pancakes[N];
    
        for ( size_t i = 0; i < N; i++ ) 
        {
            std::cout << "Please input the number of pancakes person " << i + 1 << " has eaten.\n";
            pancakes[i].first = i + 1;
            std::cin >> pancakes[i].second;
        }
    
        size_t n = N;
        while ( !( n < 2 ) )
        {
            size_t last_sorted = 0;
            for ( size_t i = 1; i < n; i++) 
            {
                if ( pancakes[i-1].second > pancakes[i].second ) 
                {
                    auto valueSwitched = pancakes[i-1];
                    pancakes[i-1] = pancakes[i];
                    pancakes[i] = valueSwitched;
                    last_sorted = i;
                }
            }
    
            n = last_sorted;
        }
    
        std::cout << "\n-----ListSorted-----\n\n";
    
        for ( size_t i = 0; i < N; i++ ) 
        {
            std::cout << "Person " << pancakes[i].first << " ate: " << pancakes[i].second << std::endl;
        }
    
        return 0;
    }
    

    对于与上面相同的输入,其排序后的输出将如下所示

    -----ListSorted-----
    
    Person 2 ate: 1
    Person 4 ate: 2
    Person 6 ate: 3
    Person 8 ate: 4
    Person 10 ate: 5
    Person 9 ate: 6
    Person 7 ate: 7
    Person 5 ate: 8
    Person 3 ate: 9
    Person 1 ate: 10
    

    【讨论】:

      猜你喜欢
      • 2013-09-09
      • 2013-09-28
      • 2016-02-10
      • 1970-01-01
      • 2021-02-23
      • 2011-09-27
      • 2020-06-24
      • 1970-01-01
      相关资源
      最近更新 更多