码迷,mamicode.com
首页 > 编程语言 > 详细

C++之算法,冒泡排序

时间:2014-11-05 16:39:07      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   color   ar   os   for   sp   div   

思路:

对一个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]<<"\n";
    }
}

 

C++之算法,冒泡排序

标签:style   blog   io   color   ar   os   for   sp   div   

原文地址:http://www.cnblogs.com/alazalazalaz/p/4076363.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!