🎓 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 (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
1. Introduction
Binary Search is an efficient search algorithm that works on the principle of divide and conquer. It's used to search for an element in a sorted list. Instead of searching the list in a linear manner, binary search divides the list into halves and determines which half of the list the search key is likely to be in, reducing the number of elements to be examined by half with each step.
2. Implementation Steps
1. Initialize two pointers: low to the beginning and high to the end of the array.
2. Find the middle element of the array using (low + high) / 2.
3. If the middle element matches the key, return the middle index.
4. If the key is smaller than the middle element, set high to middle - 1 and repeat step 2.
5. If the key is larger than the middle element, set low to middle + 1 and repeat step 2.
6. If low surpasses high, the key doesn't exist in the list.
3. Implementation in Kotlin Programming
fun binarySearch(arr: List<Int>, key: Int): Int {
var low = 0
var high = arr.size - 1
while (low <= high) {
val middle = (low + high) / 2
when {
arr[middle] == key -> return middle
arr[middle] < key -> low = middle + 1
else -> high = middle - 1
}
}
return -1
}
fun main() {
val numbers = listOf(11, 22, 34, 45, 56, 78, 90)
val key = 45
println("List: $numbers")
val result = binarySearch(numbers, key)
if (result == -1) {
println("$key is not present in the list.")
} else {
println("$key is present at index $result.")
}
}
Output:
List: [11, 22, 34, 45, 56, 78, 90] 45 is present at index 3.
Explanation:
1. The Binary Search algorithm begins by examining the middle element of a sorted list.
2. If the key is equal to the middle element, the search is successful, and the position is returned.
3. If the key is smaller than the middle element, it means the desired element lies in the first half. Thus, the algorithm continues its search only in the first half.
4. Conversely, if the key is larger, the search continues only in the second half.
5. The list gets divided in half during each iteration until the key is found or until the subarray size becomes 0, indicating the key isn't present in the list.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment