各位高手好!我是个小白!下面这是我编写的一段控制台应用程序的代码!是关于直接排序的算法的!
int[] arr = new int[14] { 12, 11, 51, 35, 18, 21, 54, 31, 47, 52, 48, 45, 32, 2 };
for (int i = 0; i < 14; ++i)
{
int temp = arr[i];
int j = i;
while ((j > 0) && (arr[j - 1] > temp) )
{
arr[j] = arr[j-1];
--j;
}
arr[j] = temp;
}
Console.WriteLine("排序后结果是: ");
foreach (int n in arr)
{
Console.WriteLine("{0}",n + " ");
}
Console.ReadLine();
我想问一下!代码中那个“++i”和“--j”是什么意思?谢谢高手们!小弟不生感激!
int[] arr = new int[14] { 12, 11, 51, 35, 18, 21, 54, 31, 47, 52, 48, 45, 32, 2 };
for (int i = 0; i < 14; ++i)
{
int temp = arr[i];
int j = i;
while ((j > 0) && (arr[j - 1] > temp) )
{
arr[j] = arr[j-1];
--j;
}
arr[j] = temp;
}
Console.WriteLine("排序后结果是: ");
foreach (int n in arr)
{
Console.WriteLine("{0}",n + " ");
}
Console.ReadLine();
我想问一下!代码中那个“++i”和“--j”是什么意思?谢谢高手们!小弟不生感激!
++i在放在这里跟i++没啥区别,都是自身加1而已。
--j在while循环里的作用就是,记录前边比当前数小的数的坐标,然后把当前值赋给数组为J的坐标。