/******************************************************************************
* 功能: 冒泡排序
* 输入: src:源数据数组
len:源数据长度
up_down: 0/1:升序/降序
******************************************************************************/
void Bubble(UINT8 *src, UINT8 len, UINT8 up_down)
{
UINT8 i,j,tmp,unchange;
if (up_down == 0) //升序
{
for (i=1; i<len; i++)
{
unchange = 1;
for (j=0; j<len-1-i; j++)
{
if (src[j] > src[j+1])
{
unchange = 0;
tmp = src[j];
src[j] = src[j+1];
src[j+1] = tmp;
}
}
if (unchange == 1) //不再有变化,无需再排
break;
}
}
else
{
for (i=1; i<len; i++)
{
unchange = 1;
for (j=0; j<len-1-i; j++)
{
if (src[j] < src[j+1])
{
unchange = 0;
tmp = src[j];
src[j] = src[j+1];
src[j+1] = tmp;
}
}
if (unchange == 1) //不再有变化,无需再排
break;
}
}
}
文章评论(0条评论)
登录后参与讨论