int[] arr = new int[14] { 12, 15, 13, 51, 26, 48, 14, 61, 57, 45, 62, 43, 3, 51 };
for (int i = 0; i < arr.Length; i++)//冒泡的次数
{
for (int j = 0; j < arr.Length-1-i; j++)//每次冒泡的具体算法
{
if (arr[j] > arr[j + 1])
{
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
foreach (int n in arr)
{
Console.WriteLine(n + " ");
}
Console.ReadLine();
各位高手好!小弟刚入门!编写了一个关于冒泡排序的控制台应用程序!
小弟想问一下!在上面的程序中每次冒泡的具体算法的那一行为什么要j<arr.Length-1-i呢?请各位高手解释一下!
百度上查不出来!
for (int i = 0; i < arr.Length; i++)//冒泡的次数
{
for (int j = 0; j < arr.Length-1-i; j++)//每次冒泡的具体算法
{
if (arr[j] > arr[j + 1])
{
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
foreach (int n in arr)
{
Console.WriteLine(n + " ");
}
Console.ReadLine();
各位高手好!小弟刚入门!编写了一个关于冒泡排序的控制台应用程序!
小弟想问一下!在上面的程序中每次冒泡的具体算法的那一行为什么要j<arr.Length-1-i呢?请各位高手解释一下!
百度上查不出来!
你用j<arr.Length-1也是可以的。
第二次i==1;arr[13]不参加比较了,将次大的数放在arr[12]。
以此类推。