最近在学习Algorithms 4th, 做个记录
题目
Binary search with only addition and subtraction. [Mihai Patrascu] Write a program that, given an array of N distinct int values in ascending order, determines whether a given integer is in the array. You may use only additions and subtractions and a constant amount of extra memory. The running time of your program should be proportional to log N in the worst case.
仅用加减实现的二分查找。编写一个程序给定一个含有N个不同int值并按照升序排序的数组,判断是否有给定的整数,只能用加法和减法以及额外的内存,程序运行时间在最坏情况下应该和logN成正比
解决方法
package day1;
import edu.princeton.cs.algs4.*;
import java.util.Arrays;
import java.util.HashSet;
public class Mihai_Patrascu {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] arr = getArr(20000);
StdOut.println();
Arrays.sort(arr);