package com.algorithm; /** * Created by nanzhou on 2017/7/20. */ public class Dichotomy { /** * 二分法 查找 时间复杂度O(logn) * @param args */ public static void main(String[] args) { int[] arr = new int[]{12, 23, 34, 45, 56, 67, 77, 89, 90}; System.out.println(search(arr, 12)); System.out.println(search(arr, 45)); System.out.println(search(arr, 67)); System.out.println(search(arr, 89)); System.out.println(search(arr, 99)); } public static int search(int[] arr, int key) { int start = 0; int end = arr.length - 1; while (start <= end) { int middle = (start + end) / 2; if (key < arr[middle]) { end = middle - 1; } else if (key > arr[middle]) { start = middle + 1; } else { return middle; } } return -1; } }
二分法 查找
最新推荐文章于 2022-12-06 22:31:08 发布