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;

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM