数组搜索(search)之线性搜索与二分法搜索

线性搜索代码比较简单,也通俗易懂,就是挨个遍历,查找数组中与key相同的元素,若查找不到,则可以返回-1(惯例,自定义),其效率为O(n).

二分法搜索代码稍稍复杂,有一个很重要的前提是数组必须是有序的。定义一个索引low(表征搜索索引下限),high(表征搜索索引上限),mid(即索引中间值)。

当key与mid位置的元素相同时,直接将mid返回,而当key大于mid位置的元素,即key若存在一定存在于后半段时,这是我们可以将mid+1赋值给low,重新计算mid。此时还是比较key与mid位置的元素,若key与mid位置的元素相同时,直接将mid返回,而当key小于mid位置的元素时,我们将mid-1赋值给high,重新计算mid。这样反复进行下去,直到low>high,此时表明数组里面没有key相同的元素。

线性搜索代码

import java.util.Random;
import java.util.Arrays;

public class LinearArray {
	private int[] data;
	private static final Random generator = new Random();
	
	public LinearArray(int size){
		data = new int[size];
		for (int i =0; i<size;i++){
			data[i] = generator.nextInt(90) + 10;
		}
	}
	public int linearSearch(int searchKey){
		for (int index = 0; index<data.length; index++){
			if (data[index] == searchKey)
				return index;
		}
		return -1;
	}
	public String toString(){
		return Arrays.toString(data);
	}
}

测试类

import java.util.Scanner;
public class LinearSearchTest {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner input = new Scanner(System.in);
		int searchInt, position;
		LinearArray searchArray = new LinearArray(10);
		System.out.println(searchArray+"\n");
		
		System.out.print("Please enter an integer value(-1 to quit):");
		searchInt = input.nextInt();
		while(searchInt != -1){
			position = searchArray.linearSearch(searchInt);
			if (position == -1)
				System.out.println("The integer "+ searchInt+" was not found.\n");
			else
				System.out.println("The integer "+searchInt+" was found in position "
						+position+"\n");
			System.out.print("please enter an integer value(-1 to quit):");
			searchInt = input.nextInt();
		}
	}

}

二分法搜索代码

import java.util.Random;
import java.util.Arrays;
public class BinarySearch {
	private int[] data;
	private static final Random generator = new Random();
	
	public BinarySearch(int size){
		data = new int[size];
		for (int i = 0; i< data.length;i++)
			data[i] = 10 + generator.nextInt(90);
		Arrays.sort(data);
	}
	
	public int binarySearch(int searchElement){
		int low = 0;
		int high = data.length-1;
		int mid = (low+high+1)/2;
		int location = -1;
		
		do{		
			if (searchElement == data[mid])
				location = mid;
			else if (searchElement <data[mid])
				high = mid -1;
			else
				low = mid + 1;
			mid = (low+high+1)/2;
			
		} while((low<=high)&&(location ==-1));
		return location;
			
	}
	public String toString(){
		return Arrays.toString(data);
	}
}

测试类

import java.util.Scanner;

public class BinarySearchTest {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner input = new Scanner(System.in);
		int searchInt, position;
		BinarySearch searchArray = new BinarySearch(10);
		System.out.println(searchArray.toString()+"\n");
		
		System.out.print("Please enter an integer value(-1 to quit):");
		searchInt = input.nextInt();
		while(searchInt != -1){
			position = searchArray.binarySearch(searchInt);
			if (position == -1)
				System.out.println("The integer "+ searchInt+" was not found.\n");
			else
				System.out.println("The integer "+searchInt+" was found in position "
						+position+"\n");
			System.out.print("please enter an integer value(-1 to quit):");
			searchInt = input.nextInt();
		}
	}

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值