📄 selectionsort.java
字号:
public class SelectionSort
{
/**
Precondition: numberUsed <= a.length;
The first numberUsed indexed variables have values.
Action: Sorts a so that a[0] <= a[1] <= ... <= a[numberUsed - 1].
*/
public static void sort(double[] a, int numberUsed)
{
int index, indexOfNextSmallest;
for (index = 0; index < numberUsed - 1; index++)
{//Place the correct value in a[index]:
indexOfNextSmallest = indexOfSmallest(index, a, numberUsed);
interchange(index,indexOfNextSmallest, a);
//a[0] <= a[1] <=...<= a[index] and these are the smallest
//of the original array elements. The remaining positions
//contain the rest of the original array elements.
}
}
/**
Returns the index of the smallest value among
a[startIndex], a[startIndex+1], ... a[numberUsed - 1]
*/
private static int indexOfSmallest(int startIndex,
double[] a, int numberUsed)
{
double min = a[startIndex];
int indexOfMin = startIndex;
int index;
for (index = startIndex + 1; index < numberUsed; index++)
if (a[index] < min)
{
min = a[index];
indexOfMin = index;
//min is smallest of a[startIndex] through a[index]
}
return indexOfMin;
}
/**
Precondition: i and j are legal indices for the array a.
Postcondition: Values of a[i] and a[j] have been interchanged.
*/
private static void interchange(int i, int j, double[] a)
{
double temp;
temp = a[i];
a[i] = a[j];
a[j] = temp; //original value of a[i]
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -