第 41 天: 顺序查找与折半查找
很基础的查找算法,好像没啥注意的
package datastructures.search;
import java.util.Arrays;
/**
* ClassName: DataArray
* Package: datastructures.search
* <p>
* Description: Data array for searching and sorting algorithms.
*
* @Author: luv_x_c
* @Create: 2023/6/1 16:26
*/
public class DataArray {
/**
* An inner class.
*/
class dataNode {
/**
* The key.
*/
int key;
/**
* The data content.
*/
String content;
/**
* The first constructor.
*/
public dataNode(int paraKey, String paraContent) {
this.key = paraKey;
this.content = paraContent;
}// Of the first constructor
@Override
public String toString() {
return "(" + key + " , " + content + ") ";
}// Of toString
}//Of class dataNode
/**
* The data array.
*/
dataNode[] data;
/**
* The length of the data array.
*/
int length;
/**
* The first constructor.
*
* @param paraKeyArray The array of the keys.
* @param paraContentArray The array of the contents.
*/
public DataArray(int[] paraKeyArray, String[] paraContentArray) {
this.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
@Override
public String toString() {
return "DataArray{" +
"data=" + Arrays.toString(data) +
", length=" + length +
'}';
}// Of toString
/**
* Sequential search. The index 0 is not used.
*
* @param paraKey The given key.
* @return The content of the key.
*/
public String sequentialSearch(int paraKey) {
// 用作岗哨,保证表里可以找到,避免越界的麻烦
data[0].key = paraKey;
int i = length - 1;
while (data[i].key != paraKey) {
i--;
}
return data[i].content;
}// Of sequentialSearch
/**
* Test unit.
*/
public static void sequentialTest() {
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
/**
* Binary search. The keys must be sorted.
*
* @param paraKey The given key.
* @return The content of the key.
*/
public String binarySearch(int paraKey) {
int tempLeft = 0;
int tempRight = length - 1;
int tempMid;
while (tempLeft <= tempRight) {
tempMid = (tempLeft + tempRight) / 2;
if (data[tempMid].key == paraKey) {
return data[tempMid].content;
} else if (data[tempMid].key > paraKey) {
tempRight = tempMid - 1;
} else {
tempLeft = tempMid + 1;
}//Of if
}// Of while
return null;
}//Of binarySearch
/**
* Test unit.
*/
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
/**
* The entrance of the program.
*
* @param args Not used now.
*/
public static void main(String[] args) {
System.out.print("The sequentialTest:\n");
sequentialTest();
System.out.println("\nThe binaryTest:");
binarySearchTest();
}
}
注意一下二分里面while循环条件应该是小于等于,而不是小于,不然会漏掉左右中间的那个