代码实现
<pre style="background-color: rgb(43, 43, 43); font-family: "JetBrains Mono", monospace; font-size: 9.8pt; color: rgb(169, 183, 198);">/**<br> * @author 小也<br> * @create 2021/4/3 8:09<br> */<br>public class BinarySearch {<br> public static void search(int[] arr,int left,int right,int x) {<br> if (x < arr[left] || x > arr[right]){<br> System.out.println("x不存在");<br> return;<br> }<br> int mid = left + (right - left)/2;<br> if (x == arr[mid]){<br> System.out.println("x存在");<br> }<br><br> if (x < arr[mid]){<br> search(arr,left,mid - 1,x);<br> }<br> if (x > arr[mid]){<br> search(arr,mid + 1,right,x);<br> }<br> }<br>}<br></pre>