package datastructure.search;
/**
* ******************************************
*
* @author Michelle Min MitchelleMin@163.com
* @date 2021-07-01
* ******************************************
*/
public class DataArray {
/*
An inner class for data nodes. The next book usually use an int value to
represent the data. I would like to use a key-value pair instead.
*/
class DataNode{
/*
The key.
*/
int key;
/*
The data content.
*/
String content;
/**
********************************
* The first constructor.
********************************
*/
DataNode(int paraKey, String paraContent){
key = paraKey;
content = paraContent;
}//of the second constructor
/**
********************************
* Overrides the method claimed in Object, the superclass of any class.
********************************
*/
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 keys.
********************************
*/
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 an 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.
*
* @param paraKey The given key.
* @return The content of the key.
********************************
*/
public String sequentialSearch(int paraKey){
data[0].key = paraKey;
int i;
//Note that we do not judge i >= 0 since data[0].key = paraKey.
//In this way the runtime is saved about 1/2.
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
/**
********************************
* Binary search. Attention: It is assume that keys are sorted in ascending
* order.
*
* @param paraKey The given key.
* @return The content of the key.
********************************
*/
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", "swith", "case", "for", "while"};
DataArray tempDataArray = new DataArray(tempSortedKeys, 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 binarySearchTest
/**
********************************
*The entrance of the program.
*
* @param args Not used now.
********************************
*/
public static void main(String args[]){
System.out.println("\r\n---------sequentialSearchTest--------");
sequentialSearchTest();
System.out.println("\r\n--------binarySearchTest-------");
binarySearchTest();
}//of main
}//of class DataArray.
day41
最新推荐文章于 2023-06-04 20:42:02 发布