C/C++教程

冒泡排序(C++)

本文主要是介绍冒泡排序(C++),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
#include <iostream>

using namespace std;

int main(void)
{
    int beauties[] = { 2, 1, 4, 6, 8, 5, 9, 7, 5 };

    int len = sizeof(beauties) / sizeof(beauties[0]);
    int test = 0;
  


    for (int j = 0; j < len ; j++)
    {
        for (int i = 0; i < len-1; i++)
        {
            if (beauties[i] > beauties[i + 1])
            {
                test = beauties[i];
                beauties[i] = beauties[i + 1];
                beauties[i + 1] = test;
                 
            }     
        }
    }


    //cout << "------------------------------------" << endl;
    for (int i = 0; i < len; i++)
    {
        cout << beauties[i] << "\t";
    }
    cout << endl;

    cin.get();

    return 0;
}

 

这篇关于冒泡排序(C++)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!