day41

Day 41

1. Background

今天是学习java的第41天,学习内容是顺序查找和折半查找。经过了前几天图的折磨,我现在看到这种简单的代码已经非常享受了。

2. Description

我觉得今天的代码没有什么可讲的,顺序查找直接无脑遍历就完事,折半查找的逻辑则是初中学的二分法。

这两个逻辑简单,然后代码的实现上也很直接,所以我今天代码完成的速度就非常的快。

里面需要注意的地方就是老师建立的数据结果是键值对了,这个东西如果是学了python的话就很熟悉,就是python里面的字典嘛。这个概念本身也简单,然后再加上我其实是学python出身的,所以对字典这种键值对的处理就非常熟练了。

最后说一句,老师在顺序查找那里的代码是真的简单,核心语句就一行。学习到了。

最后反馈一个问题

在这里插入图片描述

这是我的初版代码,编译器给我报错了。我当时一直想不出,我已经返回了一个data[i].content,这个就是string类型的值,为啥还是让我必须返回一个。

后来我反复思考,想到如果给的那个paraKey是超过范围的呢,要是if遍历完了还是没有找到那个key呢,这样方法就返回不了任何值,所以编译器才会报错。

3. Code

package datastructure.search;

public class DataArray {
    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 toString.
        public String toString() {
			return "(" + key + ", " + content + ") ";
		}// Of toString
    } // Of class DataNode

    // The data array.
    DataNode[] data;

    // The length of data array.
    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 toString,
    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

    public String sequentialSearch(int paraKey) {
        data[0].key = paraKey;

        // 这里虽然是一个简单的顺序查找,但我仍然要说一句,老师的代码真的好简单。
        // 我在写的时候就无脑遍历,完全没有下面代码这样简洁。
        int i; // 注意,这里不能把i写到for里面去,因为后面需要return data[i],所以i不能是局部变量。
        for (i = length - 1; data[i].key != paraKey; i--) {
			continue;
		}//Of for i
		return data[i].content;
    } // Of sequentialSearch

    // Test sequentialSearch.
    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

    /**
     ************
     * 折半查找,相当于数学上的二分法。有一定的局限性,必须要数据有序排列。
     * 
     * @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

    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
}

运行结果:

earchTest-------");
sequentialSearchTest();

	System.out.println("\r\n-------binarySearchTest-------");
	binarySearchTest();
}// Of main

}


运行结果:

![在这里插入图片描述](https://img-blog.csdnimg.cn/afa6eae08be9413a9ec330bd5ca8be65.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5LuK5bm05YWD5aSc5pe2,size_20,color_FFFFFF,t_70,g_se,x_16#pic_center)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
明日问题的输入事件与输出事件为: | Input Events | Node Output Event description Node . e0: start program event 1 e7: Welcome message 2 e1: center a valid month 6e8: print today's date 4 e2: enter an invalid month 67| e9: print tomorrow's date 6 e3: enter a valid day 69 e10: "month OK" 39 e4: enter an invalid day 69 e11: "month out of range" 41 e5: enter a valid year 71 e12: "day OK" 4 e6: enter an invalid year 71 e13: "day out of range" 4S e14: "year OK" 54 e15: "year out of range" 5( e16: "Date OK" 6C e17: "please enter a valid date" 62 e18: "enter a month" 6( e19: "enter a day" 68 e20: "enter a year" 70 c21: "Day is month, day, year" 8S 在下表中,ASF-6对应的输入事件为: 输出事件 ASF-7对应的输入事件为: 输出事件 为:_ ASF-8对应的输入事件为:_, 输出事件 为:_ ASF-9对应的输入事件为:_,输 出事件 为:_ Atomic System Function Inputs Outputs L ASF-1 start program e0 e7 | ASF-2 enter a date with an invalid month, valid day and valid year e2, e3,e5 e11,e12,e14,e17 | ASF-3 enter a date with an invalid day, valid month and validyear| e1, c4,e5 e10,e13,e14,e17 | ASE-4 enter a date wih an ivalid year, valid day and valid monh| el,e3, c6 e10,e12, el5,e17| ASF-5 enter a date with valid month, day, and year e1,e3,e5 . e10, e12, e14, e16, c21 | ASIF-6 enter a date with valid month, day and year invalid ASF-7 enter a date with valid day, month and year invalid ASF-8 enter a date with valid year, day and month invalid ASF-9 enter a date with invalid month, day, year
06-01

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值