Download presentation
Presentation is loading. Please wait.
Published byNigel Miller Modified over 8 years ago
1
Linear Search Linear Search is a fundamental search algorithm. Linear search, also known as sequential search, is a process that checks every element in the list sequentially until the desired element is found. 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 “Linear Search”
3
Definitions of the components/Keywords: 5 3 2 4 1 This algorithm is suitable for searching a list of data for a particular value. Linear search can be done in either a sorted or an unsorted array. In the Linear search, each element of an array is sequentially compared with the desired element to find the location of the desired element. Desired element / Item / Search value: An element that is being searched for in the array. Process to find the desired element using Linear search: 1. Length of the array is defined. 2. A counter will be initialized (say i = 0) and its value is compared with the length of the array. 3. If i is less than the length of the array, desired element is compared with the i th element of the array i. The i th element in the array is compared with the desired element. If the element matches the desired element, the location of desired element (i) is returned ii. If the element doesn’t match, the counter i is incremented to i+1 4. If i is greater than the length of the array, it means the end of the array has been reached. 5. Repeat steps 3-5 until the desired element is found or end of the array is reached
4
Definitions of the components/Keywords: 5 3 2 4 1 The advantage of linear search is its simplicity: concept is easy to understand and implementation is straight-forward. The main disadvantage of linear search is that it has a very poor efficiency. 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 general complexity of Linear search is O(n) For a list with n items, the best case is when the value is equal to the first element of the list, in which case only one comparison is needed The worst case is when the value is not in the list (or occurs only once at the end of the list), in which case n comparisons are needed.
5
Master Layout 5 3 2 4 1 Give START, PAUSE and STOP buttons 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 Fig. A Simulation Area 0 8 48 35 60 14 52 2116 01234567 Current Index: Give the value of the Index at that particular time Legend: Array Desired element Desired element Match Current Index Array or list Index ** 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).
6
Step 1: 1 5 3 2 4 Case - 1 I nstruction for the animator T ext 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 box in violet and the text “ Current Index: 0” In parallel show the text and arrow in red as shown in figure. Then blink the orange box and blue box to which red arrow is pointing to. While blinking, a text box saying “ Comparing 21 and 8” must be appeared. If the values in those 2 boxes doesn’t match, the text and the arrow in red should move rightwards. Always update the value in violet box accordingly. The text in DT should appear in parallel to the figure Array of 8 elements with indices Current index is i=0 The desired element to search for in the array is 21 21 is compared with the element at i=0 21 ≠ 8 Therefore i is incremented to i+1 The incremented i is compared with the length of the array. 0 + 1 < 8 TRUE so will go for next comparison 21 i=0 8 48 35 60 14 52 2116 0 Legend: Array Desired element Desired element Match Current Index 01234567 Current index: Comparing 21 and 8 21 Search Value:
7
Step 2: 1 5 3 2 4 I nstruction for the animator T ext to be displayed in the working area (DT) show the box in violet and the text “ Current Index: 1” In parallel show the text and arrow in red as shown in figure. Then blink the orange box and blue box to which red arrow is pointing to. While blinking, a text box saying “ Comparing 21 and 48” must be appeared. If the values in those 2 boxes doesn’t match, the text and the arrow in red should move rightwards. Always update the value in violet box accordingly. The text in DT should appear in parallel to the figure Current index is i=1 The desired element to search for in the array is 21 21 is compared with the element at i=1 21 ≠ 48 Therefore i is incremented to i+1 The incremented i is compared with the length of the array. 1 + 1 < 8 TRUE so will go for next comparison 14 13 21 5 32 6 32 21 i=1 8 48 35 60 14 52 2116 01234567 Current index: 1 Legend: Array Desired element Desired element Match Current Index Comparing 21 and 48 21 Search Value:
8
Step 3: 1 5 3 2 4 I nstruction for the animator T ext to be displayed in the working area (DT) show the box in violet and the text “ Current Index: 2” In parallel show the text and arrow in red as shown in figure. Then blink the orange box and blue box to which red arrow is pointing to. While blinking, a text box saying “ Comparing 21 and 35” must be appeared. If the values in those 2 boxes doesn’t match, the text and the arrow in red should move rightwards. Always update the value in violet box accordingly. The text in DT should appear in parallel to the figure Current index is i=2 The desired element to search for in the array is 21 21 is compared with the element at i=2 21 ≠ 35 Therefore i is incremented to i+1 The incremented i is compared with the length of the array. 2 + 1 < 8 TRUE so will go for next comparison 14 13 21 5 32 6 32 21 i=2 8 48 35 60 14 52 2116 01234567 Current index: 2 Legend: Array Desired element Desired element Match Current Index Comparing 21 and 35 21 Search Value:
9
Step 4: 1 5 3 2 4 I nstruction for the animator T ext to be displayed in the working area (DT) show the box in violet and the text “ Current Index: 3” In parallel show the text and arrow in red as shown in figure. Then blink the orange box and blue box to which red arrow is pointing to. While blinking, a text box saying “ Comparing 21 and 60” must be appeared. If the values in those 2 boxes doesn’t match, the text and the arrow in red should move rightwards. Always update the value in violet box accordingly. The text in DT should appear in parallel to the figure Current index is i=3 The desired element to search for in the array is 21 21 is compared with the element at i=3 21 ≠ 60 Therefore i is incremented to i+1 The incremented i is compared with the length of the array. 3 + 1 < 8 TRUE so will go for next comparison 14 13 21 5 32 6 32 21 i=3 8 48 35 60 14 52 2116 01234567 Current index: 3 Legend: Array Desired element Desired element Match Current Index Comparing 21 and 60 21 Search Value:
10
Step 5: 1 5 3 2 4 I nstruction for the animator T ext to be displayed in the working area (DT) show the box in violet and the text “ Current Index: 4” In parallel show the text and arrow in red as shown in figure. Then blink the orange box and blue box to which red arrow is pointing to. While blinking, a text box saying “ Comparing 21 and 14” must be appeared. If the values in those 2 boxes doesn’t match, the text and the arrow in red should move rightwards. Always update the value in violet box accordingly. The text in DT should appear in parallel to the figure Current index is i=4 The desired element to search for in the array is 21 21 is compared with the element at i=4 21 ≠ 14 Therefore i is incremented to i+1 The incremented i is compared with the length of the array. 4 + 1 < 8 TRUE so will go for next comparison 14 13 21 5 32 6 32 21 i=4 8 48 35 60 14 52 2116 01234567 Current index: 4 Legend: Array Desired element Desired element Match Current Index Comparing 21 and 14 21 Search Value:
11
Step 6: 1 5 3 2 4 I nstruction for the animator T ext to be displayed in the working area (DT) show the box in violet and the text “ Current Index: 5” In parallel show the text and arrow in red as shown in figure. Then blink the orange box and blue box to which red arrow is pointing to. While blinking, a text box saying “ Comparing 21 and 52” must be appeared. If the values in those 2 boxes doesn’t match, the text and the arrow in red should move rightwards. Always update the value in violet box accordingly. The text in DT should appear in parallel to the figure Current index is i=5 The desired element to search for in the array is 21 21 is compared with the element at i=5 21 ≠ 52 Therefore i is incremented to i+1 The incremented i is compared with the length of the array. 5 + 1< 8 TRUE so will go for next comparison 14 13 21 5 32 6 32 21 i=5 8 48 35 60 14 52 2116 01234567 Current index: 5 Legend: Array Desired element Desired element Match Current Index Comparing 21 and 52 21 Search Value:
12
Step 7: 1 5 3 2 4 I nstruction for the animator T ext to be displayed in the working area (DT) show the box in violet and the text “ Current Index: 6” In parallel show the text and arrow in red as shown in figure. Then blink the orange box and blue box to which red arrow is pointing to. While blinking, a text box saying “ Comparing 21 and 21” must be appeared. Now the values in those 2 boxes match. Once the values match change the color of both the boxes to green. Display the location of desired element. With the text “ Element found at location 6” Always update the value in violet box accordingly. The text in DT should appear in parallel to the figure Current index is i=6 The desired element to search for in the array is 21 21 is compared with the element at i=6 21 = 21 The desired element is found at location 6. 14 13 21 5 32 6 32 i=6 8 48 35 60 14 52 2116 01234567 Current index: 6 Legend: Array Desired element Desired element Match Current Index Comparing 21 and 21 21 Search Value: Element found at location 6
13
Step 8: 1 5 3 2 4 Case - 2 I nstruction for the animator T ext 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 box in violet and the text “ Current Index: 0” In parallel show the text and arrow in red as shown in figure. Then blink the orange box and blue box to which red arrow is pointing to. While blinking, a text box saying “ Comparing 30 and 8” must be appeared. If the values in those 2 boxes doesn’t match, the text and the arrow in red should move rightwards. Always update the value in violet box accordingly. The text in DT should appear in parallel to the figure Array of 8 elements with indices Current index is i=0 The desired element to search for in the array is 30 30 is compared with the element at i=0 30 ≠ 8 Therefore i is incremented to i+1 The incremented i is compared with the length of the array. 0 + 1 < 8 TRUE so will go for next comparison 30 i=0 8 48 35 60 14 52 2116 0 Legend: Array Desired element Desired element Match Current Index 01234567 Current index: Comparing 30 and 8 30 Search Value:
14
Step 9: 1 5 3 2 4 I nstruction for the animator T ext to be displayed in the working area (DT) show the box in violet and the text “ Current Index: 1” In parallel show the text and arrow in red as shown in figure. Then blink the orange box and blue box to which red arrow is pointing to. While blinking, a text box saying “ Comparing 30 and 48” must be appeared. If the values in those 2 boxes doesn’t match, the text and the arrow in red should move rightwards. Always update the value in violet box accordingly. The text in DT should appear in parallel to the figure Current index is i=1 The desired element to search for in the array is 30 30 is compared with the element at i=1 30 ≠ 48 Therefore i is incremented to i+1 The incremented i is compared with the length of the array. 1 + 1 < 8 TRUE so will go for next comparison 14 13 21 5 32 6 32 30 i=1 8 48 35 60 14 52 2116 01234567 Current index: 1 Legend: Array Desired element Desired element Match Current Index Comparing 30 and 48 30 Search Value:
15
Step 10: 10: 1 5 3 2 4 I nstruction for the animator T ext to be displayed in the working area (DT) show the box in violet and the text “ Current Index: 2” In parallel show the text and arrow in red as shown in figure. Then blink the orange box and blue box to which red arrow is pointing to. While blinking, a text box saying “ Comparing 30 and 35” must be appeared. If the values in those 2 boxes doesn’t match, the text and the arrow in red should move rightwards. Always update the value in violet box accordingly. The text in DT should appear in parallel to the figure Current index is i=2 The desired element to search for in the array is 30 30 is compared with the element at i=2 30 ≠ 35 Therefore i is incremented to i+1 The incremented i is compared with the length of the array. 2 + 1 < 8 TRUE so will go for next comparison 14 13 21 5 32 6 32 30 i=2 8 48 35 60 14 52 2116 01234567 Current index: 2 Legend: Array Desired element Desired element Match Current Index Comparing 30 and 35 30 Search Value:
16
Step 11: 1 5 3 2 4 I nstruction for the animator T ext to be displayed in the working area (DT) show the box in violet and the text “ Current Index: 3” In parallel show the text and arrow in red as shown in figure. Then blink the orange box and blue box to which red arrow is pointing to. While blinking, a text box saying “ Comparing 30 and 60” must be appeared. If the values in those 2 boxes doesn’t match, the text and the arrow in red should move rightwards. Always update the value in violet box accordingly. The text in DT should appear in parallel to the figure Current index is i=3 The desired element to search for in the array is 30 30 is compared with the element at i=3 30 ≠ 60 Therefore i is incremented to i+1 The incremented i is compared with the length of the array. 3+ 1 < 8 TRUE so will go for next comparison 14 13 21 5 32 6 32 30 i=3 8 48 35 60 14 52 2116 01234567 Current index: 3 Legend: Array Desired element Desired element Match Current Index Comparing 30 and 60 30 Search Value:
17
Step 12: 1 5 3 2 4 I nstruction for the animator T ext to be displayed in the working area (DT) show the box in violet and the text “ Current Index: 4” In parallel show the text and arrow in red as shown in figure. Then blink the orange box and blue box to which red arrow is pointing to. While blinking, a text box saying “ Comparing 30 and 14” must be appeared. If the values in those 2 boxes doesn’t match, the text and the arrow in red should move rightwards. Always update the value in violet box accordingly. The text in DT should appear in parallel to the figure Current index is i=4 The desired element to search for in the array is 30 30 is compared with the element at i=4 30 ≠ 14 Therefore i is incremented to i+1 The incremented i is compared with the length of the array. 4 + 1 < 8 TRUE so will go for next comparison 14 13 21 5 32 6 32 30 i=4 8 48 35 60 14 52 2116 01234567 Current index: 4 Legend: Array Desired element Desired element Match Current Index Comparing 30 and 14 30 Search Value:
18
Step 13: 1 5 3 2 4 I nstruction for the animator T ext to be displayed in the working area (DT) show the box in violet and the text “ Current Index: 5” In parallel show the text and arrow in red as shown in figure. Then blink the orange box and blue box to which red arrow is pointing to. While blinking, a text box saying “ Comparing 30 and 52” must be appeared. If the values in those 2 boxes doesn’t match, the text and the arrow in red should move rightwards. Always update the value in violet box accordingly. The text in DT should appear in parallel to the figure Current index is i=5 The desired element to search for in the array is 30 30 is compared with the element at i=5 30 ≠ 52 Therefore i is incremented to i+1 The incremented i is compared with the length of the array. 5 + 1< 8 TRUE so will go for next comparison 14 13 21 5 32 6 32 30 i=5 8 48 35 60 14 52 2116 01234567 Current index: 5 Legend: Array Desired element Desired element Match Current Index Comparing 30 and 52 30 Search Value:
19
Step 14: 1 5 3 2 4 I nstruction for the animator T ext to be displayed in the working area (DT) show the box in violet and the text “ Current Index: 6” In parallel show the text and arrow in red as shown in figure. Then blink the orange box and blue box to which red arrow is pointing to. While blinking, a text box saying “ Comparing 30 and 21” must be appeared. If the values in those 2 boxes doesn’t match, the text and the arrow in red should move rightwards. Always update the value in violet box accordingly. The text in DT should appear in parallel to the figure Current index is i=6 The desired element to search for in the array is 30 30 is compared with the element at i=6 30 ≠ 21 Therefore i is incremented to i+1 The incremented i is compared with the length of the array. 6 + 1 < 8 TRUE so will go for next comparison 14 13 21 5 32 6 32 i=6 8 48 35 60 14 52 2116 01234567 Current index: 6 Legend: Array Desired element Desired element Match Current Index Comparing 30 and 21 30 Search Value:
20
Step 15: 1 5 3 2 4 I nstruction for the animator T ext to be displayed in the working area (DT) show the box in violet and the text “ Current Index: 6” In parallel show the text and arrow in red as shown in figure. Then blink the orange box and blue box to which red arrow is pointing to. While blinking, a text box saying “ Comparing 30 and 16” must be appeared. If the values in those 2 boxes doesn’t match, the text and the arrow in red should move rightwards. Always update the value in violet box accordingly. After the end of the array display the text “Element not found” The text in DT should appear in parallel to the figure Current index is i=7 The desired element to search for in the array is 30 30 is compared with the element at i=7 30 ≠ 16 Therefore i is incremented to i+1 The incremented i is compared with the length of the array. 7 + 1 < 8 FALSE implies end of the array The desired element is not found in the array. 14 13 21 5 32 6 32 i=7 8 48 35 60 14 52 2116 01234567 Current index: 7 Legend: Array Desired element Desired element Match Current Index Comparing 30 and 16 30 Search Value: Element not found
21
Introduction Credits 21 Definitions Test your understanding (questionnaire) Lets Sum up (summary) Want to know more… (Further Reading) Try it yourself Interactivity: Analogy Slide 1 Slide 3 Slide 22-26 Slide 27 Electrical Engineering 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. Array Size :Values in the array: : In the input box the user will input all the numerical values that he/she wants to be in the array. Desired element Value :
22
Questionnaire 1.Which of the following are TRUE about Linear search? I. It is a sequential search II. Its complexity is O(n) III. Its efficiency is poor Answers: a) I and II b) Only I c) II and III d) I, II and III 1 5 2 4 3
23
Questionnaire 2. The Worst case occurs in linear search algorithm when Answers: a) Item is somewhere in the middle of the array b) Item is not in the array at all c) Item is the last element in the array d) Item is the last element in the array or is not there at all 1 5 2 4 3
24
Questionnaire 3. For a searching operation to be done using linear search, the array can be Answers: a) Sorted b) Unsorted c) either sorted or unsorted 1 5 2 4 3
25
Questionnaire 4. How many maximum comparisons can be done on an array of 10 elements using linear search? Answers: a) 5 b) 10 c) 20 d) None of the above 1 5 2 4 3
26
Questionnaire 5. I.The time required for a search operation depends on complexity. II. Linear search is difficult to understand which of the above are TRUE? Answers: a) I and II b) Only I c) Only II d) None 1 5 2 4 3
27
Links for further reading Reference websites: http://en.wikipedia.org/wiki/Linear_search http://video.franklin.edu/Franklin/Math/170/common/mod01/linearSearchAlg.html Books: “Introduction to Algorithm” Thomas H. Coremen
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.