1.插入排序
基本思想:每一步将一个待排序的数据插入到前面已经排好序的有序序列中,直到插完所有元素为止。
2.代码
/**
*
***********************************
* @Title: insertionSort
* @Description:
* @param:
* @return: void
***********************************
*/
public void insertionSort() {
DataNode tempNode;
int j;
for (int i = 2; i < length; i++) {
tempNode = data[i];
//find the position to insert
for (j = i - 1; data[j].key > tempNode.key; j--) {
data[j+1] = data[j];
}//Of for j
data[j+1] = tempNode;
System.out.println("Round " + (i - 1));
System.out.println(this);
}//Of for i
}//Of insertionSort
/**
*********************
* Test the method.
*********************
*/
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
/**
*********************
* The entrance of the program.
*
* @param args Not used now.
*********************
*/
public static void main(String args[]) {
System.out.println("\r\n-------insertionSortTest-------");
insertionSortTest();
}// Of main
3.总结
今日的代码部分很简单,顾今日打算对其他知识进行学习,例如以前学过的new,重写toString,this等等,之前只是大概知道是个什么意思,但是不知道具体很详细的用法等,今日想去再学习一下。