sailing

Everything...

bubble sort

// BubbleSort.cpp : Defines the entry point for the console application.

//

 

#include "stdafx.h"

#include <iostream>

 

void BubbleSort(int* a, int iLen);

void PrintArray(int a[], int iLen);

 

int _tmain(int argc, _TCHAR* argv[])

{

    int arrayToSort[] = {20, 7, 3, 4, 25, 15, 29, 12, 4, 1};

    int n = sizeof(arrayToSort)/sizeof(int);

    PrintArray(arrayToSort, n);

    BubbleSort(arrayToSort, n);

    PrintArray(arrayToSort, n);

    return 0;

}

 

void BubbleSort(int* a, int iLen)

{

    if(a==NULL || iLen<=0)

       return;

    for(int i=0;i<iLen-2;i++)

    {

       int swaped = 0;

       for(int j=iLen-1;j>=i+1;j--)

       {

           if(a[j]<a[j-1])

           {

              int temp = a[j-1];

              a[j-1] = a[j];

              a[j]= temp;

              swaped = 1;

           }

       }

    }

}

 

void PrintArray(int a[], int iLen)

{

    for(int i=1; i< iLen; i++)

    {

       std::cout<<a[i]<<' ';

    }

    std::cout<<std::endl;

}

posted on 2007-01-28 21:29 乌生鱼汤 阅读(5) 评论(0)  编辑 收藏 网摘


标题  
姓名  
主页
Email (博主才能看到) 
验证码 *  看不清,换一张 [登录][注册]
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
Google站内搜索

China-pub 计算机图书网上专卖店!6.5万品种 2-8折!
近千种 9-95 新二手计算图书火热销售中!
开发者征途系统新作:《设计模式——基于C#的工程化实现及扩展》



相关文章:

相关链接:
 

导航

统计

与我联系

搜索

 

我的标签

随笔档案(167)

Asp

Friends

其它链接

最新评论

阅读排行榜