介绍
冒泡排序是最典型的排序算法了。这里就是为了留一个纪念。正在学习算法。
实现
#include <iostream>
using namespace std;
/************************************************************************
@ 冒泡排序
@ nums -- 数组
@ length -- 数组长度
************************************************************************/
void pop_sort(int* nums,int length)
{
int temp=0;
for(int hidx=0;hidx<length-1;hidx++) //处理最大
{
for(int lidx=hidx+1;lidx<length;lidx++) //最大和其余比较
{
if (nums[lidx] > nums[hidx]) //交换
{
temp=nums[hidx];
nums[hidx]=nums[lidx];
nums[lidx]=temp;
}
}
}
}
/************************************************************************
@ test
************************************************************************/
int main()
{
int a[10]={10,20,30,40,15,12,13,14,84,21};
pop_sort(a,10);
for (int idx=0;idx<10;idx++)
{
cout<<a[idx]<<" ";
}
return 0;
}
结果