C++冒泡排序示例

/*
    冒泡排序示例
    Wirtten by: nick
    Date: 2012-10-16 12:13
*/

#include <iostream>
#include <iomanip>

using namespace std;


void bubbleSort(int list[], int last);
void bubbleUp(int list[], int current, int last);

int main()
{
    int a[10] = {3,4,6,3,2,8,4,0,5,7};

    bubbleSort(a, 9);

    for(int i=0; i<10; i++)
        cout<< setw(5) << a[i];

    return 0;
}

void bubbleSort(int list[], int last)
{
    for(int current=0; current<last; current++)
    {
        bubbleUp(list, current, last);
    }
}

void bubbleUp(int list[], int current, int last)
{
    for(int walker=last; walker>current; walker--)
    {
        if(list[walker] < list[walker-1])
        {
            int tmp = list[walker];
            list[walker] = list[walker-1];
            list[walker-1] = tmp;
        }
    }
}
posted @ 2012-10-16 12:27  wouldguan  阅读(341)  评论(0编辑  收藏  举报