1.顺序查找与折半查找
在0这个位置,设定的有一个监察哨,查找起来会节约很多的时间。直接通过key值来判断,比较简单吧。
2.代码
/**
*
*/
package search;
/**
*
* @author leeyz_1@163.com
*/
public class DataArray {
class DataNode {
int key;
/**
* the data content
*/
String content;
DataNode(int paraKey, String paraContent) {
key = paraKey;
content = paraContent;
}// Of the second constructor
public String toString() {
return "(" + key + ", " + content + ")";
}// Of toString
}// Of class DataNode
DataNode[] data;
int length;
/**
* The first constructor
*
* @param paraKeyArray The array of the keys
* @param paraContentArray The array of contents
*/
public DataArray(int[] paraKeyArray, String[] paraContentArray) {
length = paraKeyArray.length;
data = new DataNode[length];
for (int i = 0; i < length; i++) {
data[i] = new DataNode(paraKeyArray[i], paraContentArray[i]);
} // Of for i
}// Of the first constructor
/**
*********************
* Overrides the method claimed in Object, the superclass of any class.
*********************
*/
public String toString() {
String resultString = "I am a data array with " + length + " items.\r\n";
for (int i = 0; i < length; i++) {
resultString += data[i] + " ";
} // Of for i
return resultString;
}// Of toString
/**
* Sequential search. Attention: It is assume that the index 0 is NOT used.
***********************************
* @Title: sesequentialSearch
* @Description:
* @param: @param paraKey
* @param: @return
* @return: String
***********************************
*/
public String sequentialSearch(int paraKey) {
data[0].key = paraKey;
int i;
for (i = length - 1; data[i].key != paraKey; i--) {
;
} // Of for i
return data[i].content;
}// Of sequentialSearch
/**
*********************
* Test the method.
*********************
*/
public static void sequentialSearchTest() {
int[] tempUnsortedKeys = { -1, 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);
System.out.println("Search result of 10 is: " + tempDataArray.sequentialSearch(10));
System.out.println("Search result of 5 is: " + tempDataArray.sequentialSearch(5));
System.out.println("Search result of 4 is: " + tempDataArray.sequentialSearch(4));
}// Of sequentialSearchTest
/**
*
***********************************
* @Title: binarySearch
* @Description: Binary search. Attention: It is assume that keys are sorted in
* ascending order
* @param: @param paraKey
* @param: @return
* @return: String
***********************************
*/
public String binarySearch(int paraKey) {
int tempLeft = 0;
int tempRight = length - 1;
int tempMiddle = (tempLeft + tempRight) / 2;
while (tempLeft <= tempRight) {
tempMiddle = (tempLeft + tempRight) / 2;
if (data[tempMiddle].key == paraKey) {
return data[tempMiddle].content;
} else if (data[tempMiddle].key <= paraKey) {
tempLeft = tempMiddle + 1;
} else {
tempRight = tempMiddle - 1;
}
} // Of while
// Not found.
return "null";
}// Of binarySearch
/**
*********************
* Test the method.
*********************
*/
public static void binarySearchTest() {
int[] tempSortedKeys = { 1, 3, 5, 6, 7, 9, 10 };
String[] tempContents = { "if", "then", "else", "switch", "case", "for", "while" };
DataArray tempDataArray = new DataArray(tempSortedKeys, tempContents);
System.out.println(tempDataArray);
System.out.println("Search result of 10 is: " + tempDataArray.binarySearch(10));
System.out.println("Search result of 5 is: " + tempDataArray.binarySearch(5));
System.out.println("Search result of 4 is: " + tempDataArray.binarySearch(4));
}// Of binarySearchTest
/**
*
***********************************
* @Title: main
* @Description: The entrance of the program.
* @param: @param args
* @return: void
***********************************
*/
public static void main(String args[]) {
System.out.println("\r\n-------sequentialSearchTest-------");
sequentialSearchTest();
System.out.println("\r\n-------binarySearchTest-------");
binarySearchTest();
}// Of main
}
3.总结
突然从图变成查找,难度一下降低了好多好多,哈哈哈哈哈