
Efficient Binary Search Algorithm Overview
"Learn about the binary search algorithm, a smart and efficient way to find a specified value within a sorted array. Discover how binary search works, its advantages over linear search, and the step-by-step process involved in implementing this algorithm. Explore examples and illustrations to grasp the concept easily."
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
Data Structures and Algorithms Search Algorithms(II) Binary Search Data Structure _ ICT& CS Lecture 3 Ms. Hiba Sayed
Binary Search A binary search or half-interval search algorithm finds the position of a specified value Desired value ") within a sorted array. Binary search is a smart algorithm that is much more efficient than the linear search. The elements in the array must be sorted in order. In stead of testing the array s first element, it starts with the element in the middle. At each stage, the algorithm compares the searched value desired with the value of the middle element of the array. If the keys match, then a matching element has been found so its index, or position, is returned. Otherwise, if the Searched value desired value is less than the middle element's value, then the algorithm repeats its action on the sub-array to the left of the middle element or, if the input desired value is greater, on the sub-array to the right. Data Structure _ ICT& CS Lecture 3 Ms. Hiba Sayed
Binary Search Requires array elements to be in order Divides the array into three sections: middle element elements on one side of the middle element elements on the other side of the middle element If the middle element is the correct value, done. Otherwise, go to step 1. using only the half of the array that may contain the correct value. Continue steps 1. and 2. until either the value is found or there are no more elements to examine 1. 2. 3. Data Structure _ ICT& CS Lecture 3 Ms. Hiba Sayed
Binary Search - Example Array numlist2 contains: 2 3 5 11 17 23 29 Searching for the the value 11, binary search examines 11 and stops Searching for the the value 7, linear search examines 11, 3, 5, and stops Data Structure _ ICT& CS Lecture 3 Ms. Hiba Sayed
Binary Search Algorithm Set first index to 0. Set last index to the last subscript in the array. Set found to false. Set position to -1. While found is not true and first is less than or equal to last Set middle to the subscript half-way between array[first] and array[last]. If array[middle] equals the desired value Set found to true. Set position to middle. Else If array[middle] is greater than the desired value Set last to middle - 1. Else Set first to middle + 1. End If. End While. Return position. Data Structure _ ICT& CS Lecture 3 Ms. Hiba Sayed
Binary _Search() Function int Binary_Search(int array[], int size, int value) { int first = 0, // First array element last = size - 1, // Last array element middle, // Mid point of search position = -1; // Position of search value bool found = false; // Flag while (!found && first <= last) { middle = (first + last) / 2; // Calculate mid point if (array[middle] == value) // If value is found at mid { found = true; position = middle; } else if (array[middle] > value) // If value is in lower half last = middle - 1; else first = middle + 1; // If value is in upper half } return position; } Data Structure _ ICT& CS Lecture 3 Ms. Hiba Sayed
Binary Search - Tradeoffs Benefits: Much more efficient than linear search. For array of N elements, performs at most log2N comparisons Disadvantages: Requires that array elements be sorted Data Structure _ ICT& CS Lecture 3 Ms. Hiba Sayed
Complexity of search algorithms Linear Search Time complexity in worst case? If N is number of elements, Time complexity = O(N) Binary Search Time complexity in the worst case? If N is the number of elements, Time complexity = O(lg N) Data Structure _ ICT& CS Lecture 3 Ms. Hiba Sayed