/// <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# 冒泡排序
最新推荐文章于 2025-11-26 15:12:28 发布
本文介绍了一种经典的排序算法——冒泡排序,并详细解释了其工作原理及实现过程。通过具体的代码示例,展示了如何使用C#实现冒泡排序,包括如何进行数组元素的比较与交换。
2326

被折叠的 条评论
为什么被折叠?



