Skip to content

Commit 2f5bf99

Browse files
Added the implementation of the default binary search that's found in the java.util.Arrays class
1 parent 71a6890 commit 2f5bf99

2 files changed

Lines changed: 7 additions & 1 deletion

File tree

‎src/com/sketch/Main.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public static void main(String[] args){
77
int[] sortedArray = { 1, 2, 3, 5, 7, 9, 11, 14, 17, 26, 18, 37, 42, 43, 44, 47, 48, 49, 78, 227, 5556, 8976 };
88
int valueToFind = 5556;
99

10-
int binarySearchIndex = BinarySearch.search(sortedArray, valueToFind);
10+
int binarySearchIndex = BinarySearch.searchDefault(sortedArray, valueToFind);
1111

1212
System.out.println("Expected Value Index: 20");
1313
System.out.println("Actual Value Index: " + binarySearchIndex);

‎src/com/sketch/search/BinarySearch.java‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.sketch.search;
22

3+
import java.util.Arrays;
4+
35
public class BinarySearch {
46
public static int search(int[] sortedNumbers, int numberToFind){
57
int low = 0;
@@ -21,4 +23,8 @@ else if(middleNumber < numberToFind){
2123

2224
return -1;
2325
}
26+
27+
public static int searchDefault(int[] sortedNumbers, int numberToFind){
28+
return Arrays.binarySearch(sortedNumbers, numberToFind);
29+
}
2430
}

0 commit comments

Comments
 (0)