c#冒泡排序示例分享。本站提示廣大學習愛好者:(c#冒泡排序示例分享)文章只能為提供參考,不一定能成為您想要的結果。以下是c#冒泡排序示例分享正文
冒泡排序口訣:
升序(從小到年夜):兩for一if;年夜於連;相 交流;再來個for遍歷;外層N-1;內層N-1-i;
說再多不如上代碼,更顯著:
class Program
{
static void Main(string[] args)
{
int[] array = {1, 3, 5, 7, 90, 2, 4, 6, 8, 10};
array= GetSort(array);
for (int i = 0; i < array.Length; i++)
{
Console.Write(array[i] + " ");
}
Console.ReadKey();
}
/// <summary>
/// 冒泡排序
/// </summary>
/// <param name="array"></param>
/// <returns></returns>
private static int[] GetSort(int[] array)
{
//外層輪回n-1
for (int i = 0; i < array.Length-1; i++)
{
//內層輪回n-1-i
for (int j = 0; j < array.Length-1-i; j++)
{
if (array[j] > array[j+ 1])
{
int temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
return array.ToArray();
}
}