[JavaSE] array (search-binary search), javase Array

Source: Internet
Author: User

[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 ();

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.