看编程机珠 - 一维搜索

简介: Java code implementation:   Search Algorithm class -   package com.programmingpearls.study;public class SearchAlg { private static int[]...

Java code implementation:

 

Search Algorithm class -

 

package com.programmingpearls.study; public class SearchAlg { private static int[] a = new int[]{23,33,44,11,32,89, 56,77,98,123,432,97,92,11,56,20,127,132,721,321,9,8,6,14, 112,322,311,213,43,34,30,51,5,2,53,54,49,47,512,522,503,504, 607,702,700,78,74,71,81,93,101,39,59,88,77,222,227,228,122, 155,145,135,195,197,198,199,189,109,108,1119,1089,190, 155,145,135,195,197,198,199,189,109,108,1119,1089,190, 155,145,135,195,197,198,199,189,109,108,1119,1089,190, 155,145,135,195,197,198,199,189,109,108,1119,1089,190, 155,145,135,195,197,198,199,189,109,108,1119,1089,190, 155,145,135,195,197,198,199,189,109,108,1119,1089,190, 155,145,135,195,197,198,199,189,109,108,1119,1089,190, 155,145,135,195,197,198,199,189,109,108,1119,1089,190, 155,145,135,195,197,198,199,189,109,108,1119,1089,190, 155,145,135,195,197,198,199,189,109,108,1119,1089,190, 155,145,135,195,197,198,199,189,109,108,1119,1089,190, 155,145,135,195,197,198,199,189,109,108,1119,1089,190, 155,145,135,195,197,198,199,189,109,108,1119,1089,190, 155,145,135,195,197,198,199,189,109,108,1119,1089,190, 155,145,135,195,197,198,199,189,109,108,1119,1089,190, 155,145,135,195,197,198,199,189,109,108,1119,1089,190, 155,145,135,195,197,198,199,189,109,108,1119,1089,190, 155,145,135,195,197,198,199,189,109,108,1119,1089,190, 155,145,135,195,197,198,199,189,109,108,1119,1089,190, 155,145,135,195,197,198,199,189,109,108,1119,1089,190, 155,145,135,195,197,198,199,189,109,108,1119,1089,190, 155,145,135,195,197,198,199,189,109,108,1119,1089,190, 155,145,135,195,197,198,199,189,109,108,1119,1089,190, 155,145,135,195,197,198,199,189,109,108,1119,1089,190, 155,145,135,195,197,198,199,189,109,108,1119,1089,190, 155,145,135,195,197,198,199,189,109,108,1119,1089,190, 155,145,135,195,197,198,199,189,109,108,1119,1089,190, 155,145,135,195,197,198,199,189,109,108,1119,1089,190, 155,145,135,195,197,198,199,189,109,108,1119,1089,190, 155,145,135,195,197,198,199,189,109,108,1119,1089,190, 155,145,135,195,197,198,199,189,109,108,1119,1089,190, 155,145,135,195,197,198,199,189,109,108,1119,1089,190, 155,145,135,195,197,198,199,189,109,108,1119,1089,190, 155,145,135,195,197,198,199,189,109,108,1119,1089,190, 155,145,135,195,197,198,199,189,109,108,1119,1089,190, 155,145,135,195,197,198,199,189,109,108,1119,1089,190, 155,145,135,195,197,198,199,189,109,108,1119,1089,190, 155,145,135,195,197,198,199,189,109,108,1119,1089,190, 155,145,135,195,197,198,199,189,109,108,1119,1089,190, 155,145,135,195,197,198,199,189,109,108,1119,1089,190, 155,145,135,195,197,198,199,189,109,108,1119,1089,190, 155,145,135,195,197,198,199,189,109,108,1119,1089,190, 155,145,135,195,197,198,199,189,109,108,1119,1089,190, 155,145,135,195,197,198,199,189,109,108,1119,1089,190, 155,145,135,195,197,198,199,189,109,108,1119,1089,190, 155,145,135,195,197,198,199,189,109,108,1119,1089,190, 155,145,135,195,197,198,199,189,109,108,1119,1089,190, 155,145,135,195,197,198,199,189,109,108,1119,1089,190, 155,145,135,195,197,198,199,189,109,108,1119,1089,190, 155,145,135,195,197,198,199,189,109,108,1119,1089,190, 155,145,135,195,197,198,199,189,109,108,1119,1089,190, 155,145,135,195,197,198,199,189,109,108,1119,1089,190, 155,145,135,195,197,198,199,189,109,108,1119,1089,190, 155,145,135,195,197,198,199,189,109,108,1119,1089,190, 155,145,135,195,197,198,199,189,109,108,1119,1089,190, 155,145,135,195,197,198,199,189,109,108,1119,1089,190, 155,145,135,195,197,198,199,189,109,108,1119,1089,190, 155,145,135,195,197,198,199,189,109,108,1119,1089,190, 155,145,135,195,197,198,199,189,109,108,1119,1089,190, 155,145,135,195,197,198,199,189,109,108,1119,1089,190, 155,145,135,195,197,198,199,189,109,108,1119,1089,190, 155,145,135,195,197,198,199,189,109,108,1119,1089,190, 155,145,135,195,197,198,199,189,109,108,1119,1089,190, 155,145,135,195,197,198,199,189,109,108,1119,1089,190, 155,145,135,195,197,198,199,189,109,108,1119,1089,190, 155,145,135,195,197,198,199,189,109,108,1119,1089,190, 155,145,135,195,197,198,199,189,109,108,1119,1089,190, 155,145,135,195,197,198,199,189,109,108,1119,1089,190, 155,145,135,195,197,198,199,189,109,108,1119,1089,190, 155,145,135,195,197,198,199,189,109,108,1119,1089,190, 155,145,135,195,197,198,199,189,109,108,1119,1089,190, 155,145,135,195,197,198,199,189,109,108,1119,1089,190, 155,145,135,195,197,198,199,189,109,108,1119,1089,190, 155,145,135,195,197,198,199,189,109,108,1119,1089,190, 155,145,135,195,197,198,199,189,109,108,1119,1089,190, 155,145,135,195,197,198,199,189,109,108,1119,1089,190, 155,145,135,195,197,198,199,189,109,108,1119,1089,190, 155,145,135,195,197,198,199,189,109,108,1119,1089,190, 155,145,135,195,197,198,199,189,109,108,1119,1089,190, 155,145,135,195,197,198,199,189,109,108,1119,1089,190, 155,145,135,195,197,198,199,189,109,108,1119,1089,190, 155,145,135,195,197,198,199,189,109,108,1119,1089,190, 155,145,135,195,197,198,199,189,109,108,1119,1089,190, 155,145,135,195,197,198,199,189,109,108,1119,1089,190, 155,145,135,195,197,198,199,189,109,108,1119,1089,190, 155,145,135,195,197,198,199,189,109,108,1119,1089,190, 155,145,135,195,197,198,199,189,109,108,1119,1089,190, 155,145,135,195,197,198,199,189,109,108,1119,1089,190, 155,145,135,195,197,198,199,189,109,108,1119,1089,190, 155,145,135,195,197,198,199,189,109,108,1119,1089,190, 1999,2999,3999,4999,5999,6999,7999,8999,9999,99990}; private int size; public SearchAlg() { size = a.length; } public int searchMotherOne(int target) { TimeTracer mTimer = new TimeTracer(); for(int i=0; i;>

 

Utility class - Record Time overhead

 

package com.programmingpearls.study; public class TimeTracer { private long m_starttime; private long m_endtime; public TimeTracer() { startTimeStamp(); } private void startTimeStamp(){ m_starttime = System.nanoTime();//currentTimeMillis(); System.out.println(" start time = " + m_starttime); } private void endTimeStamp(){ m_endtime = System.nanoTime(); System.out.println(" end time = " + m_endtime); } public long getTimeDuration() { endTimeStamp(); long duration = m_endtime - m_starttime; System.out.println(" executing time = " + duration); return duration; } public long getStarttime() { return m_starttime; } public void setStarttime(long starttime) { this.m_starttime = starttime; } public long getEndtime() { return m_endtime; } public void setEndtime(long endtime) { this.m_endtime = endtime; } }

 

 

Programming Result :

 

 start time = 5979371955374
 end time = 5979372277202
 executing time = 321828
 start time = 5979372372186
 end time = 5979372466612
 executing time = 94426
 start time = 5979372554053
 end time = 5979372642333
 executing time = 88280 

 

 

目录
相关文章
74_搜索二维矩阵
74_搜索二维矩阵
|
3月前
|
算法
LeetCode第74题搜索二维矩阵
文章讲解了LeetCode第74题"搜索二维矩阵"的解决方案,利用二分搜索法将问题简化,并通过数学转换找到二维矩阵中的对应元素,展示了将二维问题转化为一维问题的解题技巧。
LeetCode第74题搜索二维矩阵
|
6月前
|
索引 Python
NumPy 分割与搜索数组详解
NumPy 的 `np.array_split()` 函数用于分割数组。基本语法是 `np.array_split(array, indices_or_sections, axis=None)`,它接受一个 NumPy 数组和分割参数,按指定轴进行分割。示例:将 `[1, 2, 3, 4, 5, 6]` 分割成 3 个子数组,结果是 `[[1, 2], [3, 4], [5, 6]]`。注意,超出数组范围的分割位置会导致异常,且元素数量可能根据需要调整。`np.array_split()` 返回子数组的列表。可以按列分割、使用掩码或不均匀分割。练习:将 `arr = [1, 2, 3, 4,
73 0
|
6月前
|
算法 测试技术 C#
【二分查找】【z型搜索】LeetCode240:搜索二维矩阵
【二分查找】【z型搜索】LeetCode240:搜索二维矩阵
|
6月前
|
算法 Java C++
二维矩阵搜索问题——小米面试题
二维矩阵搜索问题——小米面试题
37 0
|
6月前
|
算法
leetcode-74:搜索二维矩阵
leetcode-74:搜索二维矩阵
40 0
|
6月前
|
算法
leetcode-240:搜索二维矩阵 II
leetcode-240:搜索二维矩阵 II
39 0
|
机器学习/深度学习 算法
搜索与图论 - 搜索与图在算法中的应用【中】
搜索与图论 - 搜索与图在算法中的应用【中】
|
存储 机器学习/深度学习 算法
搜索与图论 - 搜索与图在算法中的应用【上】
搜索与图论 - 搜索与图在算法中的应用【上】
|
算法
图解LeetCode——240. 搜索二维矩阵 II
图解LeetCode——240. 搜索二维矩阵 II
6022 0