c++程序—冒泡排序法

#include<iostream>
using namespace std;
#include<string>
#include<ctime>

int main()
{
    //冒泡排序法

    int arr[10] = { 3,5,6,4,2,9,8,1,7,0 };

    cout << "排序前的数组为:" << endl;
    for (int i = 0; i < 10; i++) 
    {
        cout << arr[i] << "\t";
    }
    cout << endl;

    for (int j = 0; j <= 9; j++) 
    {
        
        for (int i = 0; i < 9-j; i++)
        {
            int max = arr[i];
            if (arr[i] > arr[i + 1])
            {
                arr[i] = arr[i + 1];
                arr[i + 1] = max;
            }
        }
        cout << "" << j + 1 << "轮排序后的数组为:" << endl;
        for (int n = 0; n < 10; n++)
        {
            cout << arr[n] << "\t";
        }
        cout << endl;

    }


    cout << "排序后的数组为:" << endl;
    for (int i = 0; i < 10; i++)
    {
        cout << arr[i] << "\t";
    }
    cout << endl;

    
    system("pause");
    return 0;

}

 

posted @ 2020-02-29 20:53  Jackie_Wang  阅读(3285)  评论(0编辑  收藏  举报