第三周作业——顺序查找和二分查找

顺序查找

用Java实现顺序查找,代码如下:

	/**
	 * Sequence Search: 顺序查找
	 * @param src 从哪个数组里找
	 * @param low 开始查找的数组下标
	 * @param high 结束查找的数组下标
	 * @param target 待查找的数
	 * @return 待查数在数组里的位置,若找不到返回-1
	 */
	public static int SSearch(int[] src, int low, int high, int target ){
		if(low>high){
			throw new RuntimeException("low great than high!");
		}
		
		//指针指向当前的数组下标
		int _p = low;
		while(_p<=high){
			if(src[_p] == target ){
				return _p;
			}
			_p++;
		}
		//在数组里没找到待查的数
		return -1;
	}


二分查找

用Java实现二分查找,代码如下:

	/**
	 * Binary Search: 二分查找
	 * @param src 已排好序的数组
	 * @param low 开始查找的数组下标
	 * @param high 结束查找的数组下标
	 * @param target 待查找的数
	 * @return 待查数在数组里的位置,若找不到返回-1
	 */
	public static int BSearch(int array[], int low, int high, int target ){
		//区间的中间位置
		int mid ;
		while(low<=high){
			mid = (low+high)/2;
			//待查数在左半区间
			if(array[mid]>target){
				high = mid - 1;
			//待查数在右半区间
			}else if(array[mid]<target){
				low = mid + 1;
			}else{
				return mid;
			}
		}
		
		return -1;
	}


测试

以上查找算法的测试代码如下:
	public static void main(String[] args) throws FileNotFoundException {
		final String path = "C:\\search";
		//待查找数据
		final String tinyW = "tinyW.txt";
		//数组src中的数据文件
		final String largeW_bubble = "largeW_bubble.txt";
						
		Scanner sc = new Scanner(new File(path,largeW_bubble));      	
		List<Integer> list = new ArrayList<Integer>();
		
		//-----------------------
    	int count = 0;
    	while(sc.hasNextInt()){
    		list.add(sc.nextInt());
    		count++;
    	}        	
    	int [] src = new int[count];        	
    	count = 0;
    	//将读到的数据转存到src数组中
    	for(int i : list){
    		src[count++] = i; 
    	}
    	
    	//-----------------------
    	sc = new Scanner(new File(path,tinyW));  
    	list = new ArrayList<Integer>();
    	count = 0;
    	while(sc.hasNextInt()){
    		list.add(sc.nextInt());
    		count++;
    	}    	    	
    	int [] targets = new int[count];    	
    	count = 0;
    	//将读到的数据转存到targets数组中
    	for(int i : list){
    		targets[count++] = i; 
    	}
    	    	
    	//开始时间
    	long startTime = System.currentTimeMillis();
    	
		//顺序查找
    	System.err.println("tinyW.txt里的数不在largeW_bubble.txt的里的有: ");
    	for(int num : targets){
    		//测试顺序查找
    		//int position = SSearch(src,0,src.length-1,num);    	
    		
    		//测试二分查找
    		int position = BSearch(src,0,src.length-1,num);    
    		
    		if(position<0){
    			System.err.print(num+"\t");
    		}
    	}    	
    	    	
    	//结束时间
    	long endTime = System.currentTimeMillis();
    	System.err.printf("\n二分查找结束,共耗时:%d毫秒", endTime-startTime); 
    	
    	sc.close();   	 
	}

运行结果

顺序查找:


二分查找:




总结

顺序查找

时间复杂度为: O(N) = N

二分查找

时间复杂度为:  O (N) = log 2 N

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值