C++之算法,冒泡排序

思路:

对一个n位数组,第一位和第二位比较,大的放后面,第二位和第三位比较,大的放后面,依次,,,,,,。到最后一位

此时,最大的已经放到最后了,再用上面的方法比较前n-1位。

#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
void bubbleSort(int array[] , int len){
    for (int i = len-1; i > 0; i--)
    {
        for (int j = 0; j < i; j++)
        {
            if (array[j]>array[j+1])
            {
                int temp = array[j];
                array[j] = array[j+1];
                array[j+1] = temp;
            }
        }
    }
}


int main ()
{
    int data[] = {34,65,12,43,67,5,78,10,3,70} , k;
    int len=sizeof(data)/sizeof(int);
    bubbleSort(data , len);
    for (int i = 0; i < len; i++)
    {
        cout<<data[i]<<"
";
    }
}
原文地址:https://www.cnblogs.com/alazalazalaz/p/4076363.html