冒泡排序法
指数组元素相邻的两个比较,将较小的那个调到前头,实现数组元素由小到大排列。
解题思路如下:
a[0]的值大于a[1],大的值往下走,因此a[0]值和a[1]交换,同理,可以得到有n个数,就要进行n-1次比较。
代码实现如下
#include <iostream>
#include<assert.h>
#include <stdio.h>
using namespace std;
#define ARRAYSIZE 10
void initarray(int *br, int n)//初始化数组
{
assert(NULL != br&&n > 0);
for (int i = 0; i < n; ++i)
{
br[i] = rand() % 100+1;// 0-100以内的随机数
}
}
void SwapInt(int *ap, int *br)//交换函数,将相邻两个,大的交换到下一个
{
int tmp = *ap;
*ap = *br;
*br = tmp;
}
void BollortArray(int *br,int n)
{
assert(br != NULL && n > 1);
for (int i = 0; i < n; i++)//for(i=0;i<n/2;++i)
{
bool tag =