算法提高 冒泡法排序

版权声明:菜鸟一枚~~ 有想法可在下面评论, 转载标明出处即可。 https://blog.csdn.net/KLFTESPACE/article/details/82808549

  输入10个数,用“冒泡法”对10个数排序(由小到大)这10个数字在100以内。

样例输入

1 3 6 8 2 7 9 0 4 5

样例输出

0 1 2 3 4 5 6 7 8 9

#include<stdio.h>
#include<iostream>
using namespace std;

int n=10;
void BubbleSortK(int  arry[]) {

    int temp;


    for (int i = 0; i < n; i++)

        for (int j = 0; j < n - i - 1; j++)

        {

            if (arry[j] > arry[j + 1])

            {

                temp = arry[j];

                arry[j] = arry[j + 1];

                arry[j + 1] = temp;

            }

        }
}

int main ()
{
    int a[n];
    for(int i=0; i<n; i++)
    {
        cin >> a[i];
    }

    BubbleSortK(a);

    for (int i=0; i<n; i++)
        cout << a[i] << " ";

    return 0;
}

猜你喜欢

转载自blog.csdn.net/KLFTESPACE/article/details/82808549