Algorithm: Binary Search

1 · Yingjun Mou · Aug. 12, 2021, 7 a.m.
notes about solving tree-related coding questions 1. Core code block There are two ways to write a binary search: (1)while loop, (2)recursion 1(1). Use a while loop // using Java int binarySearch(int[] a, int x){ int low=0; int high=a.length -1; int mid; while(low <= high){ mid = low + (high-low)/2 if(a[mid] == x){ return mid; } else if(a[mid] < x){ low = mid+1; } else{ high = mid-1; } } return -1; } # using python def binarySearch(a: List[int], x: int) -> int: lo...