code readable

This commit is contained in:
shellhub 2019-09-24 10:45:39 +08:00
parent eb0c7bcee4
commit a19ece9b0c

View File

@ -58,25 +58,23 @@ class BinarySearch implements SearchAlgorithm {
int median = (left + right) >>> 1; int median = (left + right) >>> 1;
int comp = key.compareTo(array[median]); int comp = key.compareTo(array[median]);
if (comp < 0) { if (comp == 0) {
return median;
} else if (comp < 0) {
return search(array, key, left, median - 1); return search(array, key, left, median - 1);
} } else {
if (comp > 0) {
return search(array, key, median + 1, right); return search(array, key, median + 1, right);
} }
return median;
} }
// Driver Program // Driver Program
public static void main(String[] args) { public static void main(String[] args) {
// Just generate data // Just generate data
Random r = ThreadLocalRandom.current(); Random r = ThreadLocalRandom.current();
int size = 100; int size = 100;
int maxElement = 100000; int maxElement = 100000;
Integer[] integers = IntStream.generate(() -> r.nextInt(maxElement)).limit(size).sorted().boxed().toArray(Integer[]::new); Integer[] integers = IntStream.generate(() -> r.nextInt(maxElement)).limit(size).sorted().boxed().toArray(Integer[]::new);