为什么选择排序方法以 "i = from + 1" 开始循环
why does selection sort method start loop with "i = from + 1"
我正在使用选择短方法对数组进行排序。该方法使用以 "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
而不是 i = 0
。文档:Finds the smallest element in a tail range of the array.
由于该方法找到了拖尾 from
的最小元素,因此循环仅比较位置 from
或更大位置的每个元素。由于 a[from]
是初始最小值,您可以在 from+1
.
位置开始比较
我正在使用选择短方法对数组进行排序。该方法使用以 "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
而不是 i = 0
。文档:Finds the smallest element in a tail range of the array.
由于该方法找到了拖尾 from
的最小元素,因此循环仅比较位置 from
或更大位置的每个元素。由于 a[from]
是初始最小值,您可以在 from+1
.