/// <summary>
/// bubbleSort;
///
/// /*理解其实程序就是思路的复述而已*/
/// </summary>
/// <param name="desti">目标数组</param>
/// <param name="swapTimes">交换次数</param>
public static void BubleSort(ref int[] desti, ref int swapTimes)
{
int destiLen = desti.Length;
/******各重循环各行其是即可********/
//冒泡次数,因为最后一次已经是最小,所以destiLen - 1
for(int i = 0; i < destiLen - 1; i ++)
{
bool ins = true;
//各次冒泡;前面已冒泡元素的位置无需冒泡;
for(int j = 0; j < destiLen - i - 1; j ++)
{
if(desti[j] > desti[j + 1])
{
ins = false;
Swap.Swaper(ref desti[j], ref desti[j + 1]);
swapTimes ++;
}
}
if(ins)
break;
}
}
C# 冒泡排序
最新推荐文章于 2024-09-16 10:36:43 发布