Binary Search A binary search algorithm finds the
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 Author : Phani Swathi Chitta Mentor : Aruna Adil Level(UG/PG): UG *The contents in this ppt are licensed under Creative Commons Attribution-Non. Commercial-Share. Alike 2. 5 India license
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”
Definitions of the components/Keywords: 1 2 • 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. 3 4 5
1 2 Definitions of the components/Keywords: ØProcess to find the desired element using Binary search: 1. 2. 3. 4. 3 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: 1. If searched value is less than the middle element, restrict the search to the first half (start to mid -1) of the list 2. If searched value is greater than the middle element, restrict the search to the second half (mid + 1 to end) of the list 4 5. Repeat steps 2 and 3 until 1. searched element is found or 2. sub array has no elements. This implies element is not found in the array 5
Definitions of the components/Keywords: 1 Ø Algorithm to implement binary search: int binary. Search(int arr[ ], int value, int start, int end) { while (start <= end) 2 { int middle = floor(start + end) / 2; if (arr[middle] == value) 3 return middle; else if (arr[middle] > value) end = middle - 1; else 4 start = middle + 1; } return key_not_found; 5 }
Definitions of the components/Keywords: 1 2 3 4 5 • 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).
1 2 Master Layout Simulation Area 0 Index 0 8 1 14 2 3 4 5 6 7 21 25 48 52 60 76 3 Array or list 4 Fig. A • Give START, PAUSE and STOP buttons • Give 2 radio buttons o Element found o 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 Current Indices: Give the value of the Indices i, j and mid at that particular time Legend: 5 Array Desired element Match ** 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). Current Index
1 Step 1: start 3 8 5 1 14 2 3 4 5 6 7 21 25 48 52 60 76 start: i=0 end: j=7 mid: FLOOR ([0+7]/2) = 3 Search Value: 4 j=7 i=0 0 2 Case – 1 (Element Found) 21 Instruction for the animator Legend: end Array Desired element Match Current Index Text to be displayed in the working area (DT) • When the user clicks start, show the figure in master layout without labeling. • Array of 8 elements with indices • Display array with its values inside and numbers above it. • The search value: 21 • Also show the search value till the animation is over. • Initialize start: i=0 and end : j=7 • Then show the text and arrows in red as shown in figure. • Compare i and j -- i<= j - 0<=7 • Show the violet box with values of start, end and calculation of mid value • Finding middle element • The text in DT should appear in parallel to the figure • mid = FLOOR([start +end]/2) TRUE
1 Step 2: i=0 0 start 2 8 mid=3 1 14 j=7 2 3 4 5 6 7 21 25 48 52 60 76 end 21 Comparing 21 and 25 3 start: i=0 end: j=7 mid: 3 Legend: Desired element Search Value: 4 21 Instruction for the animator Desired element Match Current Index Text to be displayed in the working area (DT) • Show the text and arrow for mid as shown in figure. • mid = FLOOR ([0+7]/2) = 3 • Then blink the orange box and blue box to which mid arrow is pointing to. • The desired element to search for in the array is 21 • 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 5 Array • 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 • 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.
1 Step 3: 0 1 8 14 i=0 2 start 2 3 4 5 6 7 21 25 48 52 60 76 j=2 0 1 8 14 2 21 end 3 start: i=0 end: j= mid -1 = 3 -1= 2 mid: FLOOR ([0+2]/2) = 1 Search Value: Legend: 21 Array Desired element 4 Desired element Match Current Index Instruction for the animator 5 Text to be displayed in the working area (DT) • Display array with its values inside and numbers above it. • Compare i and j -- i<= j - 0<=2 • Then show the text and arrows in red as shown in figure. • Finding middle element • Always update the values in violet box accordingly. • mid = FLOOR([start +end]/2) • The text in DT should appear in parallel to the figure TRUE
1 Step 4: 0 2 3 4 5 6 7 25 48 52 60 76 8 14 21 i=0 mid=1 j=2 0 start 1 8 1 2 14 21 end 21 Comparing 21 and 14 start: i=0 end: j=2 mid: 1 Search Value: Legend: 21 Array Desired element Match Current Index 4 Instruction for the animator • Show the text and arrow for mid as shown in figure. • mid = FLOOR ([0+2]/2) = 1 • Then blink the orange box and blue box to which mid arrow is pointing to. • The desired element to search for in the array is 21 • While blinking, a text box saying “ Comparing 21 and 14” must appear. 5 Text to be displayed in the working area (DT) • 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. • 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.
1 Step 5: 0 8 0 2 8 1 2 3 4 5 6 7 14 21 25 48 52 60 76 1 2 14 21 i=2 j=2 2 3 start 21 end start: i=mid +1 = 1+1 = 2 end: j=2 mid: FLOOR ([2+2]/2) = 2 Search Value: Legend: 21 Array Desired element Match Current Index 4 Instruction for the animator 5 Text to be displayed in the working area (DT) • Display array with its values inside and numbers above it. • Compare i and j -- i<= j - 0<=2 • Then show the text and arrows in red as shown in figure. • Finding middle element • Always update the values in violet box accordingly. • mid = FLOOR([start +end]/2) • The text in DT should appear in parallel to the figure TRUE
1 Step 6: 0 8 0 2 8 1 2 3 4 5 6 7 14 21 25 48 52 60 76 1 2 14 21 i=2 mid=2 j=2 2 3 start 21 start: i=2 end: j=2 mid: 2 Search Value: end Legend: 21 Instruction for the animator Desired element Match Current Index Text to be displayed in the working area (DT) • Show the text and arrow for mid as shown in figure. • mid = FLOOR ([2+2]/2) = 2 • Then blink the orange box and blue box to which mid arrow is pointing to. • The desired element to search for in the array is 21 • While blinking, a text box saying “ Comparing 21 and 21” must appear. 5 Array Desired element Comparing 21 and 21 Element found at location 2 4 21 • 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 • 21 is compared with the element at mid 21 = 21 • Therefore the search for element 21 is done • Element found at location 2.
1 Step 7: Case – 2 (Element not found) 0 start 2 3 j=7 i=0 8 1 14 2 3 4 5 6 7 21 25 48 52 60 76 end start: i=0 end: j=7 mid: FLOOR ([0+7]/2) = 3 Search Value: Legend: 65 Array Desired element 4 Desired element Match Current Index Instruction for the animator 5 Text to be displayed in the working area (DT) • When the user clicks start, show the figure in master layout without labeling. • Array of 8 elements with indices • Display array with its values inside and numbers above it. • The search value: 65 • Then show the text and arrows in red as shown in figure. • Initialize start: i=0 and end : j=7 • Show the violet box with values of start, end and calculation of mid value • Compare i and j -- i<= j - 0<=7 • The text in DT should appear in parallel to the figure • Finding middle element • mid = FLOOR([start +end]/2) TRUE
1 Step 8: i=0 0 start 8 2 3 1 14 j=7 2 3 4 5 6 7 21 25 48 52 60 76 end start: i=0 end: j=7 mid: 3 Search Value: Legend: 4 mid=3 65 Array Desired element Match Current Index Instruction for the animator 5 • 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 Text to be displayed in the working area (DT) • mid = FLOOR ([0+7/2) = 3
Step 9: 10: 1 i=0 0 start 2 8 mid=3 1 14 3 4 5 6 7 21 25 48 52 60 76 end Comparing 65 and 25 Search Value: Legend: 2 65 start: i=0 end: j=7 mid: 3 3 j=7 65 Array Desired element Match 4 Current Index Instruction for the animator • 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. 5 • 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 Text to be displayed in the working area (DT) • 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.
Step 10: 1 0 8 1 14 2 3 4 5 6 7 21 25 48 52 60 76 i=4 2 start 3 4 5 6 7 48 52 60 76 end start: i=mid +1 = 3+1 = 4 end: j= 7 mid: FLOOR ([4+7]/2) = 5 Search Value: Legend: 4 j=7 65 Array Desired element Match Current Index Instruction for the animator 5 Text to be displayed in the working area (DT) • Display array with its values inside and numbers above it. • Compare i and j -- i<= j - 4<=7 • Then show the text and arrows in red as shown in figure. • Finding middle element • Always update the values in violet box accordingly. • mid = FLOOR([start +end]/2) • The text in DT should appear in parallel to the figure TRUE
1 Step 11: 0 8 1 14 2 3 4 5 6 7 21 25 48 52 60 76 i=4 mid=5 2 3 4 start j=7 4 5 6 7 48 52 60 76 end start: i=4 end: j= 7 mid: 5 Search Value: Legend: 65 Array Desired element Match Current Index Instruction for the animator 5 • 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 Text to be displayed in the working area (DT) • mid = FLOOR ([4+7/2) = 5
1 Step 12: 0 8 1 14 2 3 4 5 6 7 21 25 48 52 60 76 i=4 mid=5 2 3 start: i=4 end: j= 7 mid: 5 start j=7 4 5 6 7 48 52 60 76 end 65 Search Value: Legend: 65 Comparing 65 and 52 Array Desired element 4 Desired element Match Current Index Instruction for the animator • 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. 5 • 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 Text to be displayed in the working area (DT) • 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.
1 2 3 Step 13: 0 8 1 14 2 3 4 5 6 7 21 25 48 4 52 5 60 6 76 7 48 52 start: i=mid+1 = 5+1 = 6 end: j= 7 mid: FLOOR ([6+7]/2) = 6 Search Value: Legend: 65 start 60 76 i=6 j=7 6 7 60 76 end Array Desired element Match 4 Current Index Instruction for the animator 5 Text to be displayed in the working area (DT) • Display array with its values inside and numbers above it. • Compare i and j -- i<= j - 6<=7 • Then show the text and arrows in red as shown in figure. • Finding middle element • Always update the values in violet box accordingly. • mid = FLOOR([start +end]/2) • The text in DT should appear in parallel to the figure TRUE
1 2 3 Step 14: 0 8 1 14 2 3 4 5 6 7 21 25 48 4 52 5 60 6 76 7 48 52 start: i=6 end: j= 7 mid: 6 76 i=6 mid=6 j=7 Search Value: Legend: 60 65 start 6 7 60 76 end Array Desired element 4 Desired element Match Current Index Instruction for the animator • Show the text and arrow for mid as shown in figure. • Always update the value in violet box accordingly. 5 • The text in DT should appear in parallel to the figure Text to be displayed in the working area (DT) • mid = FLOOR ([6+7/2) = 6
1 Step 15: 0 8 2 3 1 14 2 3 4 5 6 7 21 25 48 52 60 76 4 5 6 7 48 52 60 76 start: i=6 end: j= 7 mid: 6 i=6 mid=6 j=7 Search Value: 65 start Legend: Array end Comparing 65 and 60 Desired element Match Current Index Instruction for the animator • 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. 5 7 76 65 Desired element 4 6 60 • 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. Text to be displayed in the working area (DT) • 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.
Step 16: 1 0 8 2 14 2 3 4 5 6 7 21 25 48 52 60 76 4 5 6 7 48 52 60 76 start: i=mid+1 = 6+1 = 7 end: j= 7 mid: FLOOR ([7+7]/2) = 7 3 6 60 7 76 i=7 Search Value: 65 j=7 7 start Legend: 4 1 76 end Array Desired element Match Current Index Instruction for the animator 5 Text to be displayed in the working area (DT) • Display array with its values inside and numbers above it. • Compare i and j -- i<= j - 7<=7 • Then show the text and arrows in red as shown in figure. • Finding middle element • Always update the values in violet box accordingly. • mid = FLOOR([start +end]/2) • The text in DT should appear in parallel to the figure TRUE
1 Step 17: 0 8 1 14 2 3 4 5 6 7 21 25 48 52 60 76 4 5 6 7 48 52 60 76 2 3 6 60 start: i=7 end: j= 7 mid: 7 i=7 Search Value: 4 Array mid=7 j=7 7 65 start Legend: 7 76 76 end Desired element Match Current Index 5 Instruction for the animator • 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 Text to be displayed in the working area (DT) • mid = FLOOR ([7+7/2) = 7
Step 18: 0 1 1 8 14 2 3 4 5 6 7 21 25 48 4 52 5 60 6 76 7 48 52 start: i=0 end: j= 7 mid: 7 2 3 6 60 Search Value: Legend: 60 65 Array Desired element i=7 start 4 Instruction for the animator • 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. 5 7 76 mid=7 7 76 j=7 end 65 Desired element Match Current Index 76 Comparing 65 and 76 Text to be displayed in the working area (DT) • The desired element to search for in the array is 65 • 65 is compared with the element at mid 65 ≠ 76 and 65 > 76 • 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 • Therefore the search for element 65 is done only in the second half. • If greater then display the only blue boxes after mid • So i=mid +1 =7+1 = 8 • Here this is the end of the array. • Compare i and j -- i<= j - 8<=7 • Always update the values in violet box accordingly. • This implies array is completed. • The text in DT should appear in parallel to the figure • Element not found in the array. FALSE
Electrical Engineering Slide 1 Slide 3 Introduction Definitions Slide 27 -30 Analogy Slide 31 Want to know more… Test your understanding Lets Sum up (summary) (Further Reading) (questionnaire) Interactivity: Array Size : Values in the array: : Desired element Value : In the input box the user will input all the numerical values that he/she wants to be in the array. Try it yourself • Give a dropdown to select the array size from 2 - 8. • 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. 26 Credits
Questionnaire 1 1. Which of the following are TRUE about Binary search? 2 I. It is a sequential search II. Its complexity is O(log n) III. It is also called half- interval search 3 Answers: a) I and II 4 5 b) Only II c) II and III d) I, II and III
Questionnaire 1 2. For a searching operation to be done using binary search, the array must be 2 Answers: a) Sorted 3 4 5 b) Unsorted c) either sorted or unsorted
Questionnaire 1 3. The following statement is TRUE/ FALSE • Binary search is very efficient for large arrays. 2 3 4 5 Answers: a) TRUE b) FALSE
1 2 Questionnaire 4. I. Binary search takes more comparisons than linear search to find an element II. Time taken to find an element is more and is the main disadvantage in binary search which of the above are TRUE? 3 Answers: 4 a) I and II b) Only I c) Only II d)None 5
Links for further reading Reference websites: http: //en. wikipedia. org/wiki/Binary_search http: //video. franklin. edu/Franklin/Math/170/common/mod 01/binary. Search. Alg. html Books: “Introduction to Algorithm” Thomas H. Coremen
- Slides: 31