实现算法(冒泡排序)以组织数组

时间:2015-11-26 14:12:16

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

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

以下代码:

#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;
}

3 个答案:

答案 0 :(得分: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;
        }
    }
}

答案 1 :(得分:1)

我在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并且它有效..

答案 2 :(得分:0)

在循环的内部循环中,您正在比较和交换pancakes[x]pancakes[x + 1],而不是比较和交换pancakes[y]和煎饼[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
相关问题