冒泡排序

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

#include "stdafx.h"

#include<IOSTREAM>
#include<CSTDIO>
#include<CSTDLIB>
#include<CSTRING>
#include<CTIME>

using namespace std;

#define SIZE 10

void BubbleSort(int *a, int len)
{
	int i, j, k, temp;

	for(i = 0; i < len-1; i++)
	{
		for(j = len -1; j > i; j--)
		{
			if(a[j-1] > a[j])
			{
				temp = a[j-1];
				a[j-1] = a[j];
				a[j] = temp;
			}
		}
		cout<<"sort "<<i<<" step result"<<endl;
		
		for (k = 0; k < len; k++)
		{
			cout<<a[k]<<" ";
		}
		cout<<endl;
	}
}


int main(int argc, char* argv[])
{

	int array[SIZE], i = 0;

	srand(time(NULL));
	for (;i < SIZE; i++)
	{
		array[i] = rand() /1000 + 100;
	}

	cout<<"before sort -------------"<<endl;
	for (i = 0; i < SIZE; i++)
	{
		cout<<array[i]<<" ";
	}
	cout<<endl;
	BubbleSort(array, SIZE);

	cout<<"Sort: ------------------"<<endl;
	for (i = 0; i < SIZE; i++)
	{
		cout<<array[i]<<" ";
	}
	cout<<endl;
	
	return 0;
}

before sort -------------
125 122 129 128 123 130 108 102 124 102
sort 0 step result
102 125 122 129 128 123 130 108 102 124
sort 1 step result
102 102 125 122 129 128 123 130 108 124
sort 2 step result
102 102 108 125 122 129 128 123 130 124
sort 3 step result
102 102 108 122 125 123 129 128 124 130
sort 4 step result
102 102 108 122 123 125 124 129 128 130
sort 5 step result
102 102 108 122 123 124 125 128 129 130
sort 6 step result
102 102 108 122 123 124 125 128 129 130
sort 7 step result
102 102 108 122 123 124 125 128 129 130
sort 8 step result
102 102 108 122 123 124 125 128 129 130
Sort: ------------------
102 102 108 122 123 124 125 128 129 130
Press any key to continue
















  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值