【发布时间】:2016-10-25 01:48:44
【问题描述】:
我正在使用选择短方法对数组进行排序。该方法以一个以“i = from + 1”开头的 for 循环开始其 minimumPosition 方法。为什么它以那个开头而不是“i = 0”?
谁能帮我解释一下?
谢谢!
编辑:添加上下文
/**
Finds the smallest element in a tail range of the array.
@param a the array to sort
@param from the first position in a to compare
@return the position of the smallest element in the
range a[from] . . . a[a.length - 1]
*/
private static int minimumPosition(int[] a, int from)
{
int minPos = from;
for (int i = from + 1; i < a.length; i++)
{
if (a[i] < a[minPos]) { minPos = i; }
}
return minPos;
}
}
【问题讨论】:
-
你能分享一些代码,以便我们了解这个问题的一些背景吗?
-
你正在努力。告诉我们你为什么写
i = from + 1 -
因为您正在尝试找到最小值并且您不需要将 a[from] 与自身进行比较。基本上是一个小优化。