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  乌生鱼汤  阅读(138)  评论(0编辑  收藏  举报

导航