順時針列印
阿新 • • 發佈:2020-08-21
二分查詢演算法
public static int search(List<Integer> list, Integer target) { int len = list.size(); int startIndex = 0; int endIndex = len - 1; int midIndex = (startIndex + endIndex) % 2 == 0 ? (startIndex + endIndex) / 2 : (startIndex + endIndex) / 2 + 1; while (!target.equals(list.get(midIndex))) { if (target > list.get(midIndex)) { startIndex = midIndex + 1; if (startIndex > endIndex) { return -1; } midIndex = (startIndex + endIndex) % 2 == 0 ? (startIndex + endIndex) / 2 : (startIndex + endIndex) / 2 + 1; } else if (target < list.get(midIndex)) { endIndex = midIndex - 1; if (endIndex < 0) { return -1; } midIndex = (startIndex + endIndex) % 2 == 0 ? (startIndex + endIndex) / 2 : (startIndex + endIndex) / 2 + 1; } } return midIndex; }