Java学习笔记(day41)

一、学习内容

主题:顺序查找与折半查找

基础概念:

顺序查找:顺序查找就是从数组的第一个元素开始,依次比较,直到找到目标数据或查找失败。

折半查找:能使用折半查找的前提是数组中的数据是有序的。 
1)折半查找的原理: 
假设查找的数组区间为 [min,max],min 代表起始索引,max 代表结束索引,T 代表需要查找的值。 
第一步:确定该区间的中间位置 K;

第二步:将查找的值 T 与 array[k] 比较。若相等,查找成功返回此位置;否则确定新的查找区域,继续折半查找;

第三步:若 array[k]>T,由数组的有序性可知 array[k,k+1,……,max] 中的每个值都大于 T,故新的区间为 array[min,……,K-1],若 array[k]<T,同理可得新的查找区间为 array[k+1,……,max]。

二、代码编写

package datastructure.search;
/**
 * Data array for searching and sorting algorithms.
 * 
 * @author WeiZe 1025976860@qq.com.
 */
public class DataArray {
	/**
	 * An inner class for data nodes. The text book usually use an int value to
	 * represent the data. 
	 *
	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;

	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.
	 * 
	 *********************
	 */
	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--)
			;

		return data[i].content;
	}// Of sequentialSearch

	/**
	 *********************
	 * Test the method.
	 *********************
	 */
	public static void sequentialSearchTest() {
		int[] tempUnsortedKeys = { -1, 4, 3, 6, 10, 7, 2, 11 };
		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.
	 * 
	 *********************
	 */
	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
	
	/**
	 *********************
	 * 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值