Day26——顺序查找和折半查找

顺序查找就是遍历一遍数据集,设置第一个元素为目标元素,起一个“哨兵”的作用,不用进行越界判断,当返回的目标索引为0,则表示没有找到目标元素。

折半查找是针对有序顺序表,它对数据的要求很高,它的查找过程就像是一棵二叉树,平均时间复杂度为 O ( l o g 2 n ) O(log_2^n) O(log2n),以下是对两个数据进行折半查找模拟:
在这里插入图片描述
在这里插入图片描述
测试用例的折半查找树:
在这里插入图片描述

代码:

package day26;

public class DataArray {
	/**
	 * An inner class for data nodes.
	 */
	class DataNode {
		/**
		 * The key
		 */
		int key;

		/**
		 * The data content.
		 */
		String content;

		/**
		 * 
		 *********************
		 * The constructor.
		 *********************
		 *
		 */
		DataNode(int paraKey, String paraContent) {
			key = paraKey;
			content = paraContent;
		}// Of the 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;

	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 = paraContentArray.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

	/**
	 * 
	 *********************
	 * @Title: sequentialSearch
	 * @Description: TODO(Sequential seach.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 = length - 1;

		while (data[i--].key != paraKey)
			;
		i++;
		return data[i].content;
	}// Of sequentialSearch

	/**
	 * 
	 *********************
	 * @Title: sequentialSearchTest
	 * @Description: TODO(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: TODO(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;

		while (tempLeft <= tempRight) {
			tempMiddle = tempLeft + (tempRight - tempLeft) / 2;
			if (data[tempMiddle].key == paraKey) {
				return data[tempMiddle].content;
			} else if (data[tempMiddle].key <= paraKey) {
				tempLeft = tempMiddle + 1;
			} else {
				tempRight = tempMiddle - 1;
			} // Of if
		} // Of while

		return "null";
	}// Of binarySearch

	/**
	 * 
	 *********************
	 * @Title: binarySearchTest
	 * @Description: TODO(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: TODO(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


运行结果:

在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值