第 43 天: 插入排序
/**
* insertionSort
*/
public void insertionSort() {
for (int i = 2; i < length; i++) {
data[0] = data[i];
int j;
for (j = i - 1; data[j].key > data[0].key; j--) {
data[j + 1] = data[j];
}//Of for j
data[j + 1] = data[0];
System.out.println("Round " + (i - 1));
System.out.println(this);
}// Of for i
}// Of insertionSort
/**
* Test unit.
*/
public static void insertionSortTest() {
int[] tempUnsortedKeys = {100, 5, 3, 6, 10, 7, 1, 9};
String[] tempContents = {"null", "if", "then", "else", "switch", "case", "for", "while"};
DataArray tempDataArray = new DataArray(tempUnsortedKeys, tempContents);
System.out.println(tempDataArray);
tempDataArray.insertionSort();
System.out.println("Result\r\n" + tempDataArray);
}// Of insertionSortTest