马赛克007欢迎你

htt://shexinwei.blogbus.com

http://www.cppblog.com/shexinwei

感谢大家的支持

冒泡排序(C++)

/*
Subject:      Bubble sort
Author:       shexinwei
Date:         2010-09-28
Laguage:      C++
IDE | Tool:   GCC(DEV 5.0)
Version:      1.0
Modify Time:  2010-09-28
*/   

#include <cstdlib>
#include <iostream>
#include <fstream>
using namespace std;
//排序函数
int Bublle_sort(int data[],int n);
//打印数组函数
int print(int data[],int n);
int main(int argc, char *argv[])
{
    int data[] = { 2,1,5,3,5,2,6,2};
    int length = sizeof(data)/sizeof(data[0]);
    cout<<"初始序列:"<<endl;
    print(data,length);
    Bublle_sort(data,length);
    cout<<"最终结果:"<<endl;
    print(data,length);
    cout<<endl;
    system("pause");
    return 1;
}
int Bublle_sort(int data[],int n)
{
int tmp = 0;
for(int i = 0;i < n;i++)
{
    for(int j = 0;j < n-i-1;j++)
    {
            if(data[j]<data[j+1])
            {
               tmp = data[j];
               data[j] = data[j+1];
               data[j+1] = tmp;
            }
            else continue;
    }
    cout<<i+1<<"次排序结果:"<<endl;
    print(data,n);
    cout<<endl;
}
    return 1;
}
int print(int data[],int n)
{
    for(int i = 0;i < n;i++)
    {
            cout<<data[i]<<" ";
    }
            cout<<endl;
}

调试结果:


posted on 2010-09-28 21:46 马赛克007 阅读(1695) 评论(0)  编辑 收藏 引用


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


<2010年9月>
2930311234
567891011
12131415161718
19202122232425
262728293012
3456789

导航

统计

公告

QQ:306334649 本博客所发代码皆为作者原创,大家可以随便使用。

常用链接

留言簿(1)

随笔档案

文章分类

我的博客

搜索

最新评论

阅读排行榜

评论排行榜