Java SE 036 Java array search method and binary search analysis

(1) As long as a person does not give up on himself, the whole world will not give up on you.
(2) I am born to be of great use . (3) If I
cannot bear the suffering of learning, I must bear the suffering of life. How painful it is Deep comprehension.
(4) You must gain from doing difficult things . (
5) Spirit is the real blade.
(6) Conquering opponents twice, the first time in the heart.
(7) Writing is really not easy. If you like it or have something for you Help remember to like + follow or favorite~

Java SE 036 Java array search method and binary search analysis

1. Array lookup

public class ArraySearchTest{
    
    
	public static int search(int [] a , int value){
    
    
		for(int i = 0 ; i < a.length ; i ++){
    
    
			if(a[i] == value){
    
    
				return i;
			}			
		}
		return -1;
	}

	public static void main(String [] args){
    
    	
		int [] a = new int[]{
    
    1,4,2};
		int value = 3;
		System.out.println(search(a,value));
	}
}

This kind of search is a direct search method, the efficiency is the lowest, why is it inefficient, if the length of the array is very long, if it is 10,000, it needs to compare 10,000 times. This price is considerable.

2. Binary Search

(1) Requirements: The array to be searched must be in order. Ascending and descending order are both possible.

(2) Binary search is to divide the array into two under the premise that the array is ordered, and directly compare the middle element with the element to be searched. If the middle number is less than the number to be searched, therefore, the middle number The previous number will be less than the number to be searched, so all the numbers before the middle number are ignored. It certainly does not meet the requirements. Then take the middle number of the remaining number, and then compare it with the number to be compared again, and compare in turn, filtering out the number before the middle number.

(3) Simply put: the sorting takes the middle

public class ArrayBinarySearchTest
{
    
    
	public static int binarySearch(int [] a,int value){
    
    
		int low = 0 ; //数组中第一个元素的下标
		int high = a.length - 1;//数组中最后一个元素的下标
		//取中间元素。它是我们要跟待比较那个value值进行比较的那个元素它的索引在什么地方。
		int middle;
		//什么情况下让它一直能去进行判断,只要low不大于这个high就可以一直去判断。
		while(low <= high){
    
    
			//确定中间元素,索引的位置
			middle = (low + high)/2;
			
			for(int i = 0 ; i < a.length; i++){
    
    
				System.out.print(a[i]);
				if(i == middle){
    
    
					System.out.print("#");//标记一下中间元素的位置
				}
				System.out.print("   ");
			}
			System.out.println();

			//用中间元素与value进行比较
			if(a[middle]==value){
    
    
				return middle;
			}
			//如果没找到,有两种情况,一种是小于value,一种是中间的元素大于value,如果中间的元素小于value,则中间元素以及中间元素左边的元素都不要,
			//如果是大于的话,中间元素以及中间元素右边的也都不要
			if(a[middle]<value){
    
    
				low = middle + 1;//即将中间的元素与中间元素左边的元素都过滤掉,这时对最右边元素下标没有影响,只对最左边元素下标有影响
			}

			if(a[middle]>value){
    
    
				high = middle-1;//即将中间的元素与中间元素右边的元素都过滤掉。这时对最左边元素下标没有影响,只对最右边元素下标有影响
			}
		}
		return -1;
	}
	
	public static void main(String [] args){
    
    
		int[] a = new int[]{
    
    11,22,44,992,809,928,443,222};
		//冒泡排序
		for(int i = 0 ; i < a.length-1; i++){
    
    //为何length-1,length-1就是最后一个元素的下标
			for(int j = 0 ; j < a.length-i-1; j++){
    
    
				if(a[j]>a[j+1]){
    
    
					int temp = a[j];
					a[j] = a[j+1];
					a[j+1] = temp;
				}
			}
		}

		int value = 1;

		int index = binarySearch(a,value);
		System.out.println(index);
	}
}

Guess you like

Origin blog.csdn.net/xiogjie_67/article/details/108501189
Recommended