Home > Java > javaTutorial > body text

Java search example: binary method to find elements (code)

不言
Release: 2018-08-21 14:20:06
Original
2476 people have browsed it

The content of this article is about Java search examples: the method (code) of finding elements by binary method. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Binary search principle idea:

Search data and Ordered array Compare the middle element to determine whether it is to the left or right of the middle element. If it is on the right, adjust the minimum search index value and enter the next cycle; if it is on the left, adjust the maximum search index value and enter the next cycle; If they are equal, the current position is the location of the search data and the loop stops;

Note:

Because it is based on the size relationship between array elements To find elements, the array must be an ordered array, and the codes for ascending order (from small to large) and descending order (from large to small) will be different. This article takes ascending order as an example.

public class Dichotomy {
	
	public static void main(String[] args) {
        int [] array = {1,2,3,4,5};
        int target = 2;//即array[1]
        
        int low = 0;
	int high = array.length - 1;
	while (low <= high) {
	    int middle = (low + high) / 2;
	    if (target > array[middle]) {
	    low = middle + 1;
	    } else if (target < array[middle]) {
		    high = middle - 1;
	    } else {
		    System.out.println(middle);
		    break;
	    }
	}
    }
}
Copy after login

The following is the running result:

If it is an unordered array and uses the binary method to find elements, just sort the array first. For example, use bubble sort to sort in ascending order (from small to large).

The following is the specific code:

public class Dichotomy {
	
	public static void main(String[] args) {
        int [] array = {3,2,5,1,4};
        //排序
        int temp = 0;
		for (int time = 1; time < array.length; time++) {
			for (int i = 0; i < array.length-time; i++) {
				if (array[i+1]<array[i]) {
					temp = array[i+1];
					array[i+1] = array[i];
					array[i] = temp;
				}
			}
		}
		for (int i = 0; i < array.length; i++) {
			System.out.println(array[i]);
		}

        //二分法查找
        int target = 2;//即array[1]
        int low = 0;
        int high = array.length - 1;
		
        while (low <= high) {
	        int middle = (low + high) / 2;
	        if (target > array[middle]) {
	        low = middle + 1;
	        } else if (target < array[middle]) {
		        high = middle - 1;
	        } else {
		        System.out.println(middle);
		        break;
	        }
        }
    }
}
Copy after login

The following is the running result:


Related recommendations:

Detailed explanation of examples of binary search and binary search in java algorithm

Sample code for implementing binary search tree in java

The above is the detailed content of Java search example: binary method to find elements (code). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!