Download presentation
Presentation is loading. Please wait.
Published byKerrie York Modified over 6 years ago
1
Binary Search A binary search algorithm finds the position of a specified value within a sorted array. Binary search is a technique for searching an ordered list in which middle item is checked first and - based on that comparison – half of the data is discarded. The same procedure is then applied to the remaining half until a match is found or there are no more items left. Course Name: Design and Analysis of Algorithms Level(UG/PG): UG Author : Phani Swathi Chitta Mentor : Aruna Adil *The contents in this ppt are licensed under Creative Commons Attribution-NonCommercial-ShareAlike 2.5 India license
2
Learning Objectives After interacting with this Learning Object, the learner will be able to: Explain how to find an element in an array using “Binary Search”
3
1 2 3 4 5 Definitions of the components/Keywords:
Binary Search is also called half-interval search algorithm. The binary search is best suitable search algorithm for searching a particular value in sorted arrays. Every iteration eliminates half of the remaining possibilities because the array is sorted. This makes binary searches very efficient - even for large arrays. Desired element / Item / Search value: An element that is being searched for in the array. 2 3 4 5 3
4
1 2 3 4 5 Definitions of the components/Keywords:
Process to find the desired element using Binary search: Length of the array is defined. Get the middle element -----> floor[(start + end)/2] start: Index of the first element end: Index of the last element If the middle element equals to the searched value, the algorithm stops; otherwise, two cases are possible: If searched value is less than the middle element, restrict the search to the first half (start to mid -1) of the list If searched value is greater than the middle element, restrict the search to the second half (mid + 1 to end) of the list Repeat steps 2 and 3 until searched element is found or sub array has no elements. This implies element is not found in the array 2 3 4 5
5
1 2 3 4 5 Definitions of the components/Keywords:
Algorithm to implement binary search: int binarySearch(int arr[ ], int value, int start, int end) { while (start <= end) int middle = floor(start + end) / 2; if (arr[middle] == value) return middle; else if (arr[middle] > value) end = middle - 1; else start = middle + 1; } return key_not_found; } 1 2 3 4 5
6
1 2 3 4 5 Definitions of the components/Keywords:
The advantage of binary search is its speed of finding the desired element in large arrays when compared to sequential search. The main limitation of binary search is that it can be done only on sorted arrays. The complexity of any searching method is determined from the number of comparisons performed among the elements of the list in order to find the element. The time required for a search operation depends on the complexity of the searching algorithm. The complexity of the binary search algorithm is O(log n). 2 3 4 5
7
Master Layout 1 Give START, PAUSE and STOP buttons Give 2 radio buttons Element found Element not found Give a STEPPER button that allows the user to follow the simulation procedure step by step. After every step the simulation pauses until the STEPPER button is pressed Give a text area to display the status of the simulation Give a slider bar to control the speed of animation Simulation Area Index 2 1 2 3 4 5 6 7 8 14 21 25 48 52 60 76 3 Array or list Fig. A 4 Current Indices: Give the value of the Indices i, j and mid at that particular time Legend: Array Desired element Desired element Match Current Index 5 ** For animator: All digits written in white in the array are “elements” or “values at index n “(n refers to index number ex. value at index 4 = 14).
8
3 Step 1: 1 2 4 5 Case – 1 (Element Found) i=0 j=7 1 2 3 4 5 6 7 8 14
1 2 3 4 5 6 7 start 8 14 21 25 48 52 60 76 end 2 3 start: i=0 end: j=7 mid: FLOOR ([0+7]/2) = 3 Legend: Array Desired element Desired element Match Current Index 21 Search Value: 4 Instruction for the animator Text to be displayed in the working area (DT) When the user clicks start, show the figure in master layout without labeling. Display array with its values inside and numbers above it. Also show the search value till the animation is over. Then show the text and arrows in red as shown in figure. Show the violet box with values of start, end and calculation of mid value The text in DT should appear in parallel to the figure Array of 8 elements with indices The search value: 21 Initialize start: i=0 and end : j=7 Compare i and j -- i<= j - 0<=7 TRUE Finding middle element mid = FLOOR([start +end]/2) 5
9
Step 2: 1 i=0 mid=3 j=7 1 2 3 4 5 6 7 start 8 14 21 25 48 52 60 76 end 2 21 Comparing 21 and 25 3 start: i=0 end: j=7 mid: 3 Legend: Array Desired element Desired element Match Current Index 21 Search Value: 4 Instruction for the animator Text to be displayed in the working area (DT) Show the text and arrow for mid as shown in figure. Then blink the orange box and blue box to which mid arrow is pointing to. While blinking, a text box saying “ Comparing 21 and 25” must appear. If the values in those 2 boxes doesn’t match, then see whether the number in orange box is greater or less than the number in blue box If less then display the only blue boxes before mid Always update the values in violet box accordingly. The text in DT should appear in parallel to the figure mid = FLOOR ([0+7]/2) = 3 The desired element to search for in the array is 21 21 is compared with the element at mid 21 ≠ 25 and 21 < 25 Therefore the search for element 21 is done only in the first half . 5
10
Step 3: 1 1 2 3 4 5 6 7 8 14 21 25 48 52 60 76 i=0 j=2 2 1 2 start: i=0 end: j= mid -1 = 3-1= 2 mid: FLOOR ([0+2]/2) = 1 start 8 14 21 end 3 21 Search Value: Legend: Array Desired element Desired element Match Current Index 4 Instruction for the animator Text to be displayed in the working area (DT) Display array with its values inside and numbers above it. Then show the text and arrows in red as shown in figure. Always update the values in violet box accordingly. The text in DT should appear in parallel to the figure Compare i and j -- i<= j - 0<=2 TRUE Finding middle element mid = FLOOR([start +end]/2) 5
11
Step 4: 1 1 2 3 4 5 6 7 8 14 21 25 48 52 60 76 i=0 mid=1 j=2 start: i=0 end: j=2 mid: 1 2 1 2 8 14 21 start end 21 21 Search Value: 3 Legend: Array Desired element Desired element Match Current Index Comparing 21 and 14 4 Instruction for the animator Text to be displayed in the working area (DT) Show the text and arrow for mid as shown in figure. Then blink the orange box and blue box to which mid arrow is pointing to. While blinking, a text box saying “ Comparing 21 and 14” must appear. If the values in those 2 boxes doesn’t match, then see whether the number in orange box is greater or less than the number in blue box If greater then display the only blue boxes after mid Always update the values in violet box accordingly. The text in DT should appear in parallel to the figure mid = FLOOR ([0+2]/2) = 1 The desired element to search for in the array is 21 21 is compared with the element at mid 21 ≠ 14 and 21 > 14 Therefore the search for element 21 is done only in the second half . 5
12
Step 5: 1 1 2 3 4 5 6 7 8 14 21 25 48 52 60 76 1 2 8 14 21 start: i=mid +1 = 1+1 = 2 end: j=2 mid: FLOOR ([2+2]/2) = 2 2 i=2 j=2 2 start 21 end 3 21 Search Value: Legend: Array Desired element Desired element Match Current Index 4 Instruction for the animator Text to be displayed in the working area (DT) Display array with its values inside and numbers above it. Then show the text and arrows in red as shown in figure. Always update the values in violet box accordingly. The text in DT should appear in parallel to the figure Compare i and j -- i<= j - 0<=2 TRUE Finding middle element mid = FLOOR([start +end]/2) 5
13
Step 6: 1 1 2 3 4 5 6 7 8 14 21 25 48 52 60 76 1 2 start: i=2 end: j=2 mid: 2 8 14 21 2 mid=2 i=2 j=2 2 21 Search Value: 3 start 21 end Legend: Array Desired element Desired element Match Current Index 21 Comparing 21 and 21 Element found at location 2 4 Instruction for the animator Text to be displayed in the working area (DT) Show the text and arrow for mid as shown in figure. Then blink the orange box and blue box to which mid arrow is pointing to. While blinking, a text box saying “ Comparing 21 and 21” must appear. If the values in those 2 boxes match, change the color of both the boxes to green. Display the location of desired element. With the text “ Element found at location 2” Always update the value in violet box accordingly. The text in DT should appear in parallel to the figure mid = FLOOR ([2+2]/2) = 2 The desired element to search for in the array is 21 21 is compared with the element at mid 21 = 21 Therefore the search for element 21 is done Element found at location 2 . 5
14
3 Step 7: 1 2 4 5 Case – 2 (Element not found) i=0 j=7 1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 start 8 14 21 25 48 52 60 76 end 2 start: i=0 end: j=7 mid: FLOOR ([0+7]/2) = 3 3 65 Search Value: Legend: Array Desired element Desired element Match Current Index 4 Instruction for the animator Text to be displayed in the working area (DT) When the user clicks start, show the figure in master layout without labeling. Display array with its values inside and numbers above it. Then show the text and arrows in red as shown in figure. Show the violet box with values of start, end and calculation of mid value The text in DT should appear in parallel to the figure Array of 8 elements with indices The search value: 65 Initialize start: i=0 and end : j=7 Compare i and j -- i<= j - 0<=7 TRUE Finding middle element mid = FLOOR([start +end]/2) 5
15
Step 8: 1 i=0 mid=3 j=7 1 2 3 4 5 6 7 start 8 14 21 25 48 52 60 76 end 2 start: i=0 end: j=7 mid: 3 3 65 Search Value: Legend: Array Desired element Desired element Match Current Index 4 Instruction for the animator Text to be displayed in the working area (DT) Show the text and arrow for mid as shown in figure. Always update the value in violet box accordingly. The text in DT should appear in parallel to the figure mid = FLOOR ([0+7/2) = 3 5
16
Step 9: 10: 1 i=0 mid=3 j=7 1 2 3 4 5 6 7 start 8 14 21 25 48 52 60 76 end 2 65 start: i=0 end: j=7 mid: 3 Comparing 65 and 25 3 65 Search Value: Legend: Array Desired element Desired element Match Current Index 4 Instruction for the animator Text to be displayed in the working area (DT) Then blink the orange box and blue box to which mid arrow is pointing to. While blinking, a text box saying “ Comparing 65 and 25” must appear. If the values in those 2 boxes doesn’t match, then see whether the number in orange box is greater or less than the number in blue box If greater then display the only blue boxes after mid Always update the values in violet box accordingly. The text in DT should appear in parallel to the figure The desired element to search for in the array is 65 65 is compared with the element at mid 65 ≠ 25 and 65 > 25 Therefore the search for element 65 is done only in the second half . 5
17
Step 10: 1 1 2 3 4 5 6 7 8 14 21 25 48 52 60 76 i=4 j=7 2 4 5 6 7 start 48 52 60 76 end start: i=mid +1 = 3+1 = 4 end: j= 7 mid: FLOOR ([4+7]/2) = 5 3 65 Search Value: Legend: Array Desired element Desired element Match Current Index 4 Instruction for the animator Text to be displayed in the working area (DT) Display array with its values inside and numbers above it. Then show the text and arrows in red as shown in figure. Always update the values in violet box accordingly. The text in DT should appear in parallel to the figure Compare i and j -- i<= j - 4<=7 TRUE Finding middle element mid = FLOOR([start +end]/2) 5
18
Step 11: 1 1 2 3 4 5 6 7 8 14 21 25 48 52 60 76 i=4 mid=5 j=7 2 4 5 6 7 start 48 52 60 76 end start: i=4 end: j= 7 mid: 5 3 65 Search Value: Legend: Array Desired element Desired element Match Current Index 4 Instruction for the animator Text to be displayed in the working area (DT) Show the text and arrow for mid as shown in figure. Always update the value in violet box accordingly. The text in DT should appear in parallel to the figure mid = FLOOR ([4+7/2) = 5 5
19
Step 12: 1 1 2 3 4 5 6 7 8 14 21 25 48 52 60 76 i=4 mid=5 j=7 2 start: i=4 end: j= 7 mid: 5 4 5 6 7 start 48 52 60 76 end 65 3 65 Search Value: Comparing 65 and 52 Legend: Array Desired element Desired element Match Current Index 4 Instruction for the animator Text to be displayed in the working area (DT) Then blink the orange box and blue box to which mid arrow is pointing to. While blinking, a text box saying “ Comparing 65 and 52” must appear. If the values in those 2 boxes doesn’t match, then see whether the number in orange box is greater or less than the number in blue box If greater then display the only blue boxes after mid Always update the values in violet box accordingly. The text in DT should appear in parallel to the figure The desired element to search for in the array is 65 65 is compared with the element at mid 65 ≠ 52 and 65 > 52 Therefore the search for element 65 is done only in the second half . 5
20
Step 13: 1 1 2 3 4 5 6 7 8 14 21 25 48 52 60 76 4 5 6 7 48 52 60 76 start: i=mid+1 = 5+1 = 6 end: j= 7 mid: FLOOR ([6+7]/2) = 6 2 i=6 j=7 6 7 3 65 Search Value: start 60 76 end Legend: Array Desired element Desired element Match Current Index 4 Instruction for the animator Text to be displayed in the working area (DT) Display array with its values inside and numbers above it. Then show the text and arrows in red as shown in figure. Always update the values in violet box accordingly. The text in DT should appear in parallel to the figure Compare i and j -- i<= j - 6<=7 TRUE Finding middle element mid = FLOOR([start +end]/2) 5
21
Step 14: 1 1 2 3 4 5 6 7 8 14 21 25 48 52 60 76 4 5 6 7 48 52 60 76 2 start: i=6 end: j= 7 mid: 6 mid=6 j=7 i=6 6 7 3 65 Search Value: 60 76 end start Legend: Array Desired element Desired element Match Current Index 4 Instruction for the animator Text to be displayed in the working area (DT) Show the text and arrow for mid as shown in figure. Always update the value in violet box accordingly. The text in DT should appear in parallel to the figure mid = FLOOR ([6+7/2) = 6 5
22
Step 15: 1 1 2 3 4 5 6 7 8 14 21 25 48 52 60 76 4 5 6 7 48 52 60 76 2 start: i=6 end: j= 7 mid: 6 mid=6 j=7 i=6 6 7 3 60 76 end 65 start Search Value: 65 Legend: Array Desired element Desired element Match Current Index Comparing 65 and 60 4 Instruction for the animator Text to be displayed in the working area (DT) Then blink the orange box and blue box to which mid arrow is pointing to. While blinking, a text box saying “ Comparing 65 and 60” must appear. If the values in those 2 boxes doesn’t match, then see whether the number in orange box is greater or less than the number in blue box If greater then display the only blue boxes after mid Always update the values in violet box accordingly. The text in DT should appear in parallel to the figure The desired element to search for in the array is 65 65 is compared with the element at mid 65 ≠ 60 and 65 > 60 Therefore the search for element 65 is done only in the second half . 5
23
Step 16: 1 1 2 3 4 5 6 7 8 14 21 25 48 52 60 76 4 5 6 7 48 52 60 76 2 start: i=mid+1 = 6+1 = 7 end: j= 7 mid: FLOOR ([7+7]/2) = 7 6 7 60 76 3 i=7 j=7 65 Search Value: 7 start 76 end Legend: Array Desired element Desired element Match Current Index 4 Instruction for the animator Text to be displayed in the working area (DT) Display array with its values inside and numbers above it. Then show the text and arrows in red as shown in figure. Always update the values in violet box accordingly. The text in DT should appear in parallel to the figure Compare i and j -- i<= j - 7<=7 TRUE Finding middle element mid = FLOOR([start +end]/2) 5
24
Step 17: 1 1 2 3 4 5 6 7 8 14 21 25 48 52 60 76 4 5 6 7 48 52 60 76 2 6 7 60 76 start: i=7 end: j= 7 mid: 7 3 mid=7 i=7 j=7 65 7 Search Value: start 76 end Legend: Array Desired element Desired element Match Current Index 4 Instruction for the animator Text to be displayed in the working area (DT) Show the text and arrow for mid as shown in figure. Always update the value in violet box accordingly. The text in DT should appear in parallel to the figure mid = FLOOR ([7+7/2) = 7 5
25
Step 18: 1 2 3 4 5 6 7 1 8 14 21 25 48 52 60 76 4 5 6 7 48 52 60 76 start: i=0 end: j= 7 mid: 7 2 6 7 60 76 mid=7 65 Search Value: i=7 j=7 3 Legend: Array Desired element Desired element Match Current Index 7 start 76 end 65 Comparing 65 and 76 4 Instruction for the animator Text to be displayed in the working area (DT) Then blink the orange box and blue box to which mid arrow is pointing to. While blinking, a text box saying “ Comparing 65 and 76” must appear. If the values in those 2 boxes doesn’t match, then see whether the number in orange box is greater or less than the number in blue box If greater then display the only blue boxes after mid Here this is the end of the array. Always update the values in violet box accordingly. The text in DT should appear in parallel to the figure The desired element to search for in the array is 65 65 is compared with the element at mid 65 ≠ 76 and 65 > 76 Therefore the search for element 65 is done only in the second half . So i=mid +1 =7+1 = 8 Compare i and j -- i<= j - 8<=7 FALSE This implies array is completed. Element not found in the array. 5
26
Test your understanding
Electrical Engineering Slide 1 Slide 3 Slide 27-30 Slide 31 Introduction Definitions Analogy Test your understanding (questionnaire) Lets Sum up (summary) Want to know more… (Further Reading) Interactivity: Try it yourself Array Size : Values in the array: : Give a dropdown to select the array size from Place an input box and an ENTER button to enter the values in the array. After ENTER is pressed, show the array with indices on the top in the simulation area. Give another input box to enter the desired element value After the desired element value is entered, show the value in the simulation area. After desired element value is shown, enable START button. Once the animation is started, then enable PAUSE and RESET buttons. Desired element Value : In the input box the user will input all the numerical values that he/she wants to be in the array. 26 Credits
27
Questionnaire 1 Which of the following are TRUE about Binary search? It is a sequential search Its complexity is O(log n) It is also called half- interval search Answers: a) I and II b) Only II c) II and III d) I, II and III 2 3 4 5
28
Questionnaire 1 2. For a searching operation to be done using binary search, the array must be Answers: a) Sorted b) Unsorted c) either sorted or unsorted 2 3 4 5
29
4 Questionnaire 1 2 3 5 3. The following statement is TRUE/ FALSE
Binary search is very efficient for large arrays. Answers: a) TRUE b) FALSE 2 3 4 5
30
Questionnaire 1 4. Binary search takes more comparisons than linear search to find an element Time taken to find an element is more and is the main disadvantage in binary search which of the above are TRUE? Answers: a) I and II b) Only I c) Only II d)None 2 3 4 5
31
Links for further reading
Reference websites: Books: “Introduction to Algorithm” Thomas H. Coremen
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.