C++古典的なソートアルゴリズムを実現

3623 ワード

バブルソート
#include
using namespace std;


int main()
{
    int a[100];  //     
    int n;       //           
    cout << "" << endl;
    cin >> n;

    //     
    for(int i = 0; i < n; i++)
    {
        cout << "    " << i + 1 <<"     " << endl;
        cin >> a[i];
    }

    //          
    //n - 1  
    for(int i = 0; i < n - 1; i++)
    {
        //            
        //n - 1 - i
        for(int j = 0; j < n - 1 - i;j++)   //
        {
            if(a[j] > a[j+1])   //
            {
                swap(a[j],a[j+1]);
            }
        }


    }
    cout << "" << endl;
    for(int i = 0; i < n; i++)
    {
        cout << a[i] << '\t';   //        
    }
}

ソートの選択