[JavaSE] array (search-binary search), javase Array
The prerequisite array must be ordered.
Defines the smallest, largest, and intermediate badge index.
int min,max,mid; min=0; max=arr.length-1; mid=(min+max)/2;
The index above needs to change and uses a loop. condition: When the intermediate value is not equal to the target value
int min,max,mid; min=0; max=arr.length-1; mid=(min+max)/2; while(arr[mid]!=key){ if(key<arr[mid]){ }else if(arr[mid]<key){ } }
When the median value is greater than the target value, the maximum badge moves to the center badge-1 position.
When the median value is smaller than the target value, the minimum badge moves to the center badge + 1
The center badge continues to be divided into two parts
int min,max,mid; min=0; max=arr.length-1; mid=(min+max)/2; while(arr[mid]!=key){ if(key<arr[mid]){ max=mid-1; }else if(arr[mid]<key){ min=mid+1; } mid=(min+max)/2; } return mid;
At this time, the Code has a problem. When the target cannot be found, it will fall into an endless loop and add a judgment.
If you cannot find it all the time, the minimum and maximum badge will be misplaced.
int min,max,mid; min=0; max=arr.length-1; mid=(min+max)/2; while(arr[mid]!=key){ if(key<arr[mid]){ max=mid-1; }else if(arr[mid]<key){ min=mid+1; } if(min>max) return -1; mid=(min+max)/2; } return mid;
Java version:
Public class ArrayDemo {/*** @ param args */public static void main (String [] args) {int [] arr = new int, 8, 9}; System. out. println ("index:" + keySearch (arr, 7); // index: 3 System. out. println ("index:" + helfSearch (arr, 7); // index: 3}/*** Binary Search * @ param arr * @ param key * @ return */public static int helfSearch (int [] arr, int key) {int min, max, mid; min = 0; max = arr. length-1; mid = (min + max)/2; while (arr [mid]! = Key) {if (key <arr [mid]) {max = mid-1;} else if (arr [mid] <key) {min = mid + 1 ;} if (min> max) return-1; mid = (min + max)/2;} return mid ;} /*** obtain the position where the value first appears in the array * @ param arr * @ param num * @ return */public static int keySearch (int [] arr, int num) {for (int I = 0; I <arr. length; I ++) {if (arr [I] = num) {return I ;}} return-1 ;}}
PHP version:
<? Phpclass ArrayDemo {public static function main () {$ arr = array (,); echo "index :". arrayDemo: keySearch ($ arr, 7); // index: 3 echo "index :". arrayDemo: helfSearch ($ arr, 7); // index: 3}/*** Binary Search * @ param arr * @ param key * @ return */public static function helfSearch ($ arr, $ key) {$ min = 0; $ max = count ($ arr)-1; $ mid = ceil ($ min + $ max)/2); while ($ arr [$ mid]! = $ Key) {if ($ key <$ arr [$ mid]) {$ max = $ mid-1;} else if ($ arr [$ mid] <$ key) {$ min = $ mid + 1 ;}$ mid = ceil ($ min + $ max)/2); if ($ min> $ max) return-1 ;} return $ mid;}/*** get the position where the value first appears in the array * @ param arr * @ param num * @ return */public static function keySearch ($ arr, $ key) {for ($ I = 0; $ I <count ($ arr); $ I ++) {if ($ arr [$ I] = $ key) {return $ I ;}} return-1 ;}} ArrayDemo: main ();