Find:
1. Basic lookup: array elements unordered (find tail from Scratch)
2, binary lookup (binary lookup): array elements ordered pS: the elements of the array must be in order, from small to large or from large to Small. The following analysis is an array of small to large
Two-point Lookup Analysis:
A: first the array is half (that is, set min index to 0,max Index to arr.length-1, and then half index mid for (min+max)/2)
B: Compare the data you need to find in X with arr[mid]
A: The values of both are equal and return to the mid index
B: The two Differ:
1, if x > arr[mid], the value of min index is changed to: min = mid+1, then mid to be re-assigned: (MIN+MAX)/2
2, if x < arr[mid], the value of the Max index is changed to: max = mid-1, then mid to be re-assigned: (MIN+MAX)/2
C: Repeat B's action
D: note: in order to avoid finding a nonexistent data, there will be a dead loop, so add a constraint in B:
If (min > max) {
return-1;
}
E: the above is defined as a method:
A: Return type: int (because you want the index of the data you are looking for)
B: parameter list: int[] arr (the array You are looking for) and int x (x is the data you are looking for)
1 Importjava.util.Scanner;2 public classarraydemo1{3 4 public Static voidmain (string[] Args) {5 //define an array6 int[] arr = {12,31,34,45,56,78,80,87};7 //Create keyboard input8Scanner sc =NewScanner (system.in);9System.out.println ("please Enter the data you want to find:");Ten intx =Sc.nextint (); one a //Calling Methods - intindex =Choose (arr,x); -SYSTEM.OUT.PRINTLN ("the Index of the data you are looking for is:" +index); the } - - //define the method of binary lookup - public Static intChooseint[] arr,intx) { + - //define the value of the Min,max,mid index + intMin = 0; a intmax = Arr.length-1; at intMid = (min + MAX)/2; - - //Compare the data you need to find in X with arr[mid] - //The two are not equal: - while(x! =arr[mid]) { - //1. If x > arr[mid] in if(x >arr[mid]) { -Min = mid + 1; to } + //2. If x < arr[mid] - Else if(x <arr[mid]) { themax = Mid-1; * } $ //add judgment to prevent the cycle of deathPanax Notoginseng if(min >max) { - return-1; the } + //In either case, mid has to be re-assigned aMid = (min + MAX)/2; the } + //if x = arr[mid], return to mid - returnmid; $ } $ -}
2, two points to find the method of Attention:
Note: There is a problem with this Approach.
Because the array itself is unordered, lookups in this case cannot use a binary lookup.
Although sorted first, the most primitive index of the element has been changed when sorting.
So later encountered unordered array, to find, or to use the basic search method
1 public classArrayDemo2 {2 public Static voidmain (string[] Args) {3 //Defining Arrays4 int[] arr = {24, 69, 80, 57, 13 };5 6 //Sort First7 Bubblesort (arr);8 //after finding9 intindex = GetIndex (arr, 80);TenSystem.out.println ("index:" +index); one } a - //Bubble Sort Code - public Static voidBubblesort (int[] Arr) { the for(intx = 0; X < arr.length-1; X + +) { - for(inty = 0; Y < arr.length-1-x; y++) { - if(arr[y] > Arr[y + 1]) { - inttemp =arr[y]; +arr[y] = arr[y + 1]; -Arr[y + 1] =temp; + } a } at } - } - - //Two-point Search - public Static intGetIndex (int[] arr,intValue) { - //define maximum index, minimum index in intmax = Arr.length-1; - intMin = 0; to + //Calculate the intermediate index - intMid = (max + MIN)/2; the * //Compare the value of the intermediate index with the value you want to find $ while(arr[mid]! =Value) {Panax Notoginseng if(arr[mid] >Value) { -max = Mid-1; the}Else if(arr[mid] <Value) { +Min = mid + 1; a } the + //Add Judgment - if(min >max) { $ return-1; $ } - -Mid = (max + MIN)/2; the } - Wuyi returnmid; the } -}
Java 13-1 Array Advanced Two-level Lookup