冒泡排序(C++)

冒泡排序(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;
}

调试结果:


你可能感兴趣的:(冒泡排序(C++))