Javasearch algorithms commonly used in--sequential lookup and binary search
First, sequential search:
A) Principle: sequential search is in order to search from start to finish, find the data, the early end of the search, can not find the search continues until the last data.
b) Legend Description: Raw data:int[] a={4,6,2,8,1,9,0,3}; to find numbers:8
Code Demo:
ImportJava.util.Scanner;/** Sequential Search*/ Public classSequelsearch { Public Static voidMain (string[] arg) {int[] a={4,6,2,8,1,9,0,3}; Scanner input=NewScanner (system.in); System.out.println ("Please enter the number you are looking for:"); //The statement that holds the console input intnum=Input.nextint (); //Call the Searc () method to save the return value in result intresult=search (A, num); if(Result==-1) {System.out.println ("The number you entered does not exist with the array. "); } ElseSystem.out.println ("The number you entered exists, the position in the array is:" + (result+1) + "X");} Public Static intSearchint[] A,intnum) { for(inti = 0; i < a.length; i++) { if(A[i] = = num) {//If the data exists returnI//returns the index of the data, which is the location } } return-1;//If it doesn't exist, return-1}}
Run:
Two or two points find
A) Prerequisites: Find in sorted array
b) The basic idea of binary search is: First determine the middle point position of the search interval: int mid = (low+upper)/2, and then compare the value to be found with the value of the midpoint position: if equal, the lookup succeeds and returns this position. If the median position value is greater than the unknown origin value, the new find interval is the left-hand area of the middle point position. If the median position value is less than the unknown origin value, the new find interval is the right area of the middle point position. The next lookup is for the new search interval.
c) Description of the legend:
Raw data: int[] a={5,3,6,1,9,8,2,4,7}; Find out if the number 8exists;
The first step is to sort the array in ascending order by the Sort method you learned earlier:int[] a={1,2,3,4,5,6,7,8,9};
The second step, take the middle number:5 compared with 8 ,8 is greater than 5 , take the middle number to the right of the array to compare, that is {6,7,8,9}
Step three: Repeat the first and second steps until you find the data or compare all the data.
d) Code example:
ImportJava.util.Scanner;/** Two-point search*/ Public classBinarySearch { Public Static voidMain (string[] args) {int[] arr={5,3,6,1,9,8,2,4,7}; //print out raw array data firstSYSTEM.OUT.PRINTLN ("Raw array data is as follows:"); for(intN:arr) {System.out.print (n+" "); } System.out.println (); //first, sort the arrays, with bubbles sorted here for(inti=0;i<arr.length-1;i++){ for(intj=0;j<arr.length-1-i;j++){ if(arr[j]>arr[j+1]){ inttemp=Arr[j]; ARR[J]=arr[j+1]; Arr[j+1]=temp; } } } //iterate through the sorted array of outputsSystem.out.println ("After bubbling sorted array:"); for(intN:arr) {System.out.print (n+" "); } System.out.println ();//line BreakScanner input=NewScanner (system.in); System.out.println ("Please enter the number you are looking for:"); intnum=Input.nextint (); intresult=binarysearch (arr, num); if(Result==-1) {System.out.println ("The number you are looking for does not exist ..."); } Else{System.out.println ("The number you are looking for exists in the array where the position is:" +result); } } //binary search Algorithm Public Static intBinarySearch (int[] arr,intnum) { intLow=0; intUpper=arr.length-1; while(low<=Upper) { intMid= (Upper+low)/2; if(arr[mid]<num) { Low=mid+1; } Else if(arr[mid]>num) {Upper=mid-1; } Else returnmid; } return-1; }}
Run:
Common search Algorithms in Java--sequential and binary lookups