📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (176K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Let's first we will discuss some basics like what is searching ? and why we need to search?.
What is Searching?
Why do we need Searching?
Binary Search with Example
How Binary Search Works?
mid = low + (high - low) / 2
low = mid + 1
mid = low + (high - low) / 2
I conclude that the target value 10 is stored at location 5.
Binary Search Implementation using Java
- Iterative implementation
- Recursive Implementation
- Using Arrays.binarySearch()
- Using Collections.binarySearch()
Iterative Implementation
public class BinarySearch {
public int binarySearchIteratively(int[] sortedArray, int key) {
int low = 0;
int high = sortedArray.length - 1;
int index = Integer.MAX_VALUE;
while (low <= high) {
int mid = (low + high) / 2;
if (sortedArray[mid] < key) {
low = mid + 1;
} else if (sortedArray[mid] > key) {
high = mid - 1;
} else if (sortedArray[mid] == key) {
index = mid;
break;
}
}
return index;
}
public static void main(String[] args) {
int[] sortedArray = { 0, 1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 9 };
int key = 6;
BinarySearch binSearch = new BinarySearch();
int index = binSearch.binarySearchIteratively(sortedArray, key);
System.out.println("Search element found " + key+ " in location index : " + index);
}
}
public class BinarySearch {
public int binarySearchIteratively(int[] sortedArray, int key) {
int low = 0;
int high = sortedArray.length - 1;
int index = Integer.MAX_VALUE;
while (low <= high) {
int mid = (low + high) / 2;
if (sortedArray[mid] < key) {
low = mid + 1;
} else if (sortedArray[mid] > key) {
high = mid - 1;
} else if (sortedArray[mid] == key) {
index = mid;
break;
}
}
return index;
}
public static void main(String[] args) {
int[] sortedArray = { 0, 1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 9 };
int key = 6;
BinarySearch binSearch = new BinarySearch();
int index = binSearch.binarySearchIteratively(sortedArray, key);
System.out.println("Search element found " + key+ " in location index : " + index);
}
}
Search element 6 found in location index : 7
Recursive Implementation
Now, let’s have a look at a simple, recursive implementation as well:
public class BinarySearch {
public int binarySearchRecursively(int[] sortedArray, int key, int low, int high) {
int middle = (low + high) / 2;
if (high < low) {
return -1;
}
if (key == sortedArray[middle]) {
return middle;
} else if (key < sortedArray[middle]) {
return binarySearchRecursively(sortedArray, key, low, middle - 1);
} else {
return binarySearchRecursively(sortedArray, key, middle + 1, high);
}
}
public static void main(String[] args) {
int[] sortedArray = { 0, 1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 9 };
int key = 6;
BinarySearch binSearch = new BinarySearch();
int index = binSearch.binarySearchRecursively(sortedArray, key, 0, sortedArray.length -1);
System.out.println("Search element found in location index : " + index);
}
}
public class BinarySearch {
public int binarySearchRecursively(int[] sortedArray, int key, int low, int high) {
int middle = (low + high) / 2;
if (high < low) {
return -1;
}
if (key == sortedArray[middle]) {
return middle;
} else if (key < sortedArray[middle]) {
return binarySearchRecursively(sortedArray, key, low, middle - 1);
} else {
return binarySearchRecursively(sortedArray, key, middle + 1, high);
}
}
public static void main(String[] args) {
int[] sortedArray = { 0, 1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 9 };
int key = 6;
BinarySearch binSearch = new BinarySearch();
int index = binSearch.binarySearchRecursively(sortedArray, key, 0, sortedArray.length -1);
System.out.println("Search element found in location index : " + index);
}
}
Search element found in location index : 7
Using Arrays.binarySearch()
public class BinarySearch {
public int runBinarySearchUsingJavaArrays(int[] sortedArray, Integer key) {
int index = Arrays.binarySearch(sortedArray, key);
return index;
}
public static void main(String[] args) {
int[] sortedArray = { 0, 1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 9 };
int key = 6;
BinarySearch binSearch = new BinarySearch();
int index1 = binSearch.runBinarySearchUsingJavaArrays(sortedArray, key);
System.out.println("Search element found in location index : " + index1);
}
}
public class BinarySearch {
public int runBinarySearchUsingJavaArrays(int[] sortedArray, Integer key) {
int index = Arrays.binarySearch(sortedArray, key);
return index;
}
public static void main(String[] args) {
int[] sortedArray = { 0, 1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 9 };
int key = 6;
BinarySearch binSearch = new BinarySearch();
int index1 = binSearch.runBinarySearchUsingJavaArrays(sortedArray, key);
System.out.println("Search element found in location index : " + index1);
}
}
Search element found in location index : 7
Using Collections.binarySearch()
public class BinarySearch {
public int runBinarySearchUsingJavaCollections(List<Integer> sortedList, Integer key) {
int index = Collections.binarySearch(sortedList, key);
return index;
}
public static void main(String[] args) {
Integer[] sortedArray = { 0, 1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 9 };
int key = 6;
BinarySearch binarySearch = new BinarySearch();
int index1 = binarySearch.runBinarySearchUsingJavaCollections(Arrays.asList(sortedArray), key);
System.out.println("Search element found in location index : " + index1);
}
}
public class BinarySearch {
public int runBinarySearchUsingJavaCollections(List<Integer> sortedList, Integer key) {
int index = Collections.binarySearch(sortedList, key);
return index;
}
public static void main(String[] args) {
Integer[] sortedArray = { 0, 1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 9 };
int key = 6;
BinarySearch binarySearch = new BinarySearch();
int index1 = binarySearch.runBinarySearchUsingJavaCollections(Arrays.asList(sortedArray), key);
System.out.println("Search element found in location index : " + index1);
}
}
Search element found in location index : 7
T(n) = T(n/2) + c
I likes your blog very much..Simple ,informative,clean awesome work
ReplyDeleteHey Ripu, thanks for appreciation.
DeleteYou are doing great work, keep it up.
ReplyDeletenice website.