public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); int[] arr = {6, 12, 33, 87, 90, 97, 108, 561}; int i = binarySearch(arr, 87); for (int j = 0; j <arr.length ; j++) { Log.d("hh",arr[j]+""); } } public static int binarySearch(int[] arr, int x) { int low = 0; int high = arr.length - 1; while (low <= high) { int middle = (low + high) / 2; if (x == arr[middle]) { return middle; } else if (x < arr[middle]) { high = middle - 1; } else { low = middle + 1; } } return -1; } }
二分算法
最新推荐文章于 2024-08-02 18:57:38 发布