您的位置:首页 > 其它

快速排序、冒泡排序

2011-01-18 08:39 260 查看
快速排序:

int Partition(int aValue[10], int low, int high) //完成一趟快速排序

{

int pivot = aValue[low];

while (low < high)

{

while (low < high && aValue[high] >= pivot)

{

high--;

}

int tmp = aValue[high];

aValue[high] = aValue[low];

aValue[low] = tmp;

while (low < high && aValue[low] <= pivot)

{

low++;

}

tmp = aValue[high];

aValue[high] = aValue[low];

aValue[low] = tmp;

}

return low;

}

void MySort(int aValue[10], int low, int high) //递归整个排序

{

if (low < high)

{

int pivot = Partition(aValue, low, high);

MySort(aValue, low, pivot-1);

MySort(aValue, pivot+1, high);

}

}

冒泡排序:

<pre name="code" class="cpp">	for (i = 0; i < m; i++)
{
for (j = 0; j < m - i - 1; j++)//注意减1
{
if (sta[j] > sta[j+1])
{
nTmp = sta[j];
sta[j] = sta[j+1];
sta[j+1] = nTmp;
}
}
}



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐