实现快速排序之二分法:
package com.test;
/**
* 二分法
*
* @author Administrator
* @date 2018/6/10
*/
public class TestTwoDivide {
public static void main(String[] args) {
int value = 5;
int[] arrayNumber = {1, 2, 4, 5, 7, 9, 10, 12};
int result = getNumberBy2Divide(arrayNumber, value);
System.out.println("result:" + result);
}
public static int getNumberBy2Divide(int[] arrayNumber, int value){
int beginIndex = 0;
int endIndex = arrayNumber.length - 1;
int middle;
while (beginIndex <= endIndex) {
middle = (beginIndex + endIndex) / 2;
if (value == arrayNumber[middle]) {
return middle;
} else if (value < arrayNumber[middle]) {
endIndex = middle - 1;
} else {
beginIndex = middle + 1;
}
}
return -1;
}
}