Binary search · חיפוש ביינארי
Binary search
The array is sorted. Return the index of target, or -1 if it isn't there. Binary search halves the range each step: check the middle element, then keep looking in just the left or right half. Far faster than scanning every element.
חיפוש בינארי
המערך מסודר. החזר את האינדקס של target, או -1 אם אינו קיים. חיפוש בינארי מחלק את הטווח למחצית בכל צעד: בדוק את האלמנט במרכז, ולאחר מכן המשך לחפש רק בחצי השמאלי או הימני. זה הרבה מהר יותר מסריקה של כל אלמנט.
Complete binarySearch(int[] a, int target) for a sorted array a: return the index of target, or -1 if absent. Use binary search. · השלם את binarySearch(int[] a, int target) למערך מסודר a: החזר את המקדם של target, או -1 אם לא נמצא. השתמש בחיפוש בינארי.
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.