- 插入排序是简单直接的排序方式之一. 代码非常短。
- 每次保证前 i 个数据是有序的。
- 先做简单的事情 (第 1 轮最多有 1 次移动), 再做麻烦的事情 (最后一轮最多有n−1次移动)。
- 下标 0 的数据为岗哨, 与 41 天内容同理. 比其它排序方式多用一个空间。
- empNode只分配了引用(指针)的空间,并未new。
使用了第41天的代码,贴在这里:
package java41to50;
import java41to50.D41_DataArray.DataNode;
public class D41_DataArray {
class DataNode {
int key;
String content;
DataNode(int paraKey, String paraContent) {
key = paraKey;
content = paraContent;
}
public String toString() {
return "(" + key + ", " + content + ") ";
}
}
DataNode[] data;
int length;
public static void main(String[] args) {
System.out.println("\r\n-------顺序搜索测试-------");
sequentialSearchTest();
System.out.println("\r\n-------二进制搜索测试-------");
binarySearchTest();
}
public D41_DataArray(int[] keyArray, String[] contentArray) {
length = keyArray.length;
data = new DataNode[length];
for (int i = 0; i < length; i++) {
data[i] = new DataNode(keyArray[i], contentArray[i]);
}
}
public D41_DataArray(int[] paraKeyArray, String[] paraContentArray, int paraLength) {
length = paraLength;
data = new DataNode[length];
for (int i = 0; i < length; i++) {
data[i] = null;
}
int tempPosition;
for (int i = 0; i < paraKeyArray.length; i++) {
tempPosition = paraKeyArray[i] % paraLength;
while (data[tempPosition] != null) {
tempPosition = (tempPosition + 1) % paraLength;
System.out.println("碰撞,向前移动 " + paraKeyArray[i]);
}
data[tempPosition] = new DataNode(paraKeyArray[i], paraContentArray[i]);
}
}
public String hashSearch(int paraKey) {
int tempPosition = paraKey % length;
while (data[tempPosition] != null) {
if (data[tempPosition].key == paraKey) {
return data[tempPosition].content;
}
System.out.println("Not this one for " + paraKey);
tempPosition = (tempPosition + 1) % length;
}
return "null";
}
public static void hashSearchTest() {
int[] tempUnsortedKeys = { 16, 33, 38, 69, 57, 95, 86 };
String[] tempContents = { "if", "then", "else", "switch", "case", "for", "while" };
D41_DataArray tempDataArray = new D41_DataArray(tempUnsortedKeys, tempContents, 19);
System.out.println(tempDataArray);
System.out.println("Search result of 95 is: " + tempDataArray.hashSearch(95));
System.out.println("Search result of 38 is: " + tempDataArray.hashSearch(38));
System.out.println("Search result of 57 is: " + tempDataArray.hashSearch(57));
System.out.println("Search result of 4 is: " + tempDataArray.hashSearch(4));
}
public String toString() {
String resultString = "数据数组的项目个数:" + length + " .\r\n";
for (int i = 0; i < length; i++) {
resultString += data[i] + " ";
}
return resultString;
}
public String sequentialSearch(int key) {
data[0].key = key;
int i;
for (i = length - 1; data[i].key != key; i--) {
;
}
return data[i].content;
}
public static void sequentialSearchTest() {
int[] unsortedKeys = { -1, 5, 3, 6, 10, 7, 1, 9 };
String[] contents = { "null", "if", "then", "else", "switch", "case", "for", "while" };
D41_DataArray dataArray = new D41_DataArray(unsortedKeys, contents);
System.out.println(dataArray);
System.out.println("Search result of 10 is: " + dataArray.sequentialSearch(10));
System.out.println("Search result of 5 is: " + dataArray.sequentialSearch(5));
System.out.println("Search result of 4 is: " + dataArray.sequentialSearch(4));
}
public String binarySearch(int key) {
int left = 0;
int right = length - 1;
int middle = (left + right) / 2;
while (left <= right) {
middle = (left + right) / 2;
if (data[middle].key == key) {
return data[middle].content;
} else if (data[middle].key <= key) {
left = middle + 1;
} else {
right = middle - 1;
}
}
return "null";
}
public static void binarySearchTest() {
int[] sortedKeys = { 1, 3, 5, 6, 7, 9, 10 };
String[] contents = { "if", "then", "else", "switch", "case", "for", "while" };
D41_DataArray dataArray = new D41_DataArray(sortedKeys, contents);
System.out.println(dataArray);
System.out.println("Search result of 10 is: " + dataArray.binarySearch(10));
System.out.println("Search result of 5 is: " + dataArray.binarySearch(5));
System.out.println("Search result of 4 is: " + dataArray.binarySearch(4));
}
public void insertionSort() {
DataNode tempNode;
int j;
for (int i = 2; i < length; i++) {
tempNode = data[i];
for (j = i - 1; data[j].key > tempNode.key; j--) {
data[j + 1] = data[j];
}
data[j + 1] = tempNode;
System.out.println("Round " + (i - 1));
System.out.println(this);
}
}
public static void insertionSortTest() {
int[] tempUnsortedKeys = { -100, 5, 3, 6, 10, 7, 1, 9 };
String[] tempContents = { "null", "if", "then", "else", "switch", "case", "for", "while" };
D41_DataArray tempDataArray = new D41_DataArray(tempUnsortedKeys, tempContents);
System.out.println(tempDataArray);
tempDataArray.insertionSort();
System.out.println("结果\r\n" + tempDataArray);
}
}
第43天代码:
package java41to50;
public class D43_insertionSort {
public static void main(String[] args) {
System.out.println("\r\n-------插入排序测试-------");
D41_DataArray.insertionSortTest();
}
}
结果输出:
-------插入排序测试-------
数据数组的项目个数:8 .
(-100, null) (5, if) (3, then) (6, else) (10, switch) (7, case) (1, for) (9, while)
Round 1
数据数组的项目个数:8 .
(-100, null) (3, then) (5, if) (6, else) (10, switch) (7, case) (1, for) (9, while)
Round 2
数据数组的项目个数:8 .
(-100, null) (3, then) (5, if) (6, else) (10, switch) (7, case) (1, for) (9, while)
Round 3
数据数组的项目个数:8 .
(-100, null) (3, then) (5, if) (6, else) (10, switch) (7, case) (1, for) (9, while)
Round 4
数据数组的项目个数:8 .
(-100, null) (3, then) (5, if) (6, else) (7, case) (10, switch) (1, for) (9, while)
Round 5
数据数组的项目个数:8 .
(-100, null) (1, for) (3, then) (5, if) (6, else) (7, case) (10, switch) (9, while)
Round 6
数据数组的项目个数:8 .
(-100, null) (1, for) (3, then) (5, if) (6, else) (7, case) (9, while) (10, switch)
结果
数据数组的项目个数:8 .
(-100, null) (1, for) (3, then) (5, if) (6, else) (7, case) (9, while) (10, switch)