📄 generalizedselectionsort.java
字号:
public class GeneralizedSelectionSort
{
/**
Precondition: numberUsed <= a.length;
The first numberUsed indexed variables have values.
Action: Sorts a so that a[0, a[1], ... , a[numberUsed - 1] are in
increasing order by the compareTo method.
*/
public static void sort(Comparable[] 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] are correctly ordered 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,
Comparable[] a, int numberUsed)
{
Comparable min = a[startIndex];
int indexOfMin = startIndex;
int index;
for (index = startIndex + 1; index < numberUsed; index++)
if (a[index].compareTo(min) < 0)//if a[index] is less than 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, Comparable[] a)
{
Comparable 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 + -