Binary Search is an algorithm that helps us find an element in a sorted array in O(log n) time. Its algorithm works on the principle of Divide and Conquer and it works only when the available data is sorted.
Now, when we are using binary search three situation arise:
- If the middle element is the element to be searched, we return the index of the middle element.
- If the middle element is smaller than the element to be searched, we search in the sub-array to the right (Mid to End) in the same way (Get the new middle and check all the three cases again). We search in the right sub-array because the data is sorted, thus all elements before middle element will also be either less than or equal to the middle element.
- If the middle element is greater than the element to be searched, we search in the sub-array to the left(Start to Mid).
This process is continued until either we find the element to be searched or the size of the sub-array reduces to zero.
There are three ways to perform Binary Search in Scala.
In recursive approach, we recursively call for the implemented binary search algorithm with updated values of start and end until either we match the middle element with the element to be searched or the array size reduces to zero. Below is the code for the recursive approach of Binary Search in Scala.
Element found at Index 3
In iterative approach, we run a while loop until we either find the element to be searched or the array size reduces to zero. Below is the code for iterative approach of Binary Search in Scala.
Element found at Index 5
Pattern Matching And Functional Programming Approach
In this, we first match the middle element with the element to be searched. If the element is present, we return the index of it. Otherwise, we keep calling the created function with the updated parameters. Below is the code for the approach:
Element not found
- Insert a node in Binary Search Tree Iteratively
- Complexity Analysis of Binary Search
- Binary Search Tree | Set 3 (Iterative Delete)
- How to get card vertically centered between search bar and bottom of the viewport?
- How to use jQuery to Search and Replace HTML elements?
- 2-3 Trees | (Search and Insert)
- Indexed Sequential Search
- PHP multidimensional array search by value
- Python | Linear search on list or tuples
- How to search by key=>value in a multidimensional array in PHP ?
- HTML | DOM Input Search Object
- Node | URL.search API
- Node | urlObject.search API
- How to detect search engine bots with PHP ?
- How to perform a real time search and filter on a HTML table?
- How to search the max value of an attribute in an array object ?
- Scala short <(x: Short): Boolean
- Scala short <(x: Char): Boolean
- Scala Extractors
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.