Preview

04 - Linear Search

 1. In computer science, a linear search is also sometimes referred to as a:

  Mixed Search

  Easy Search

  Sequential search

  Indexed Search

 2. The linear search is simply a method for….

  …finding a list within a target

  ...finding a target value within a list

  …finding all the elements of a list and sorting them

  …sorting a list

 3. Linear search runs in at worst linear time and makes at most n comparisons, where n is ….

  the number of the first element

  the length of the list

  the number of the second element

  the letter 'n'

 4. Analyse the code below - what is the search criteria here?
#demonstration of linear search

def linearsearch(x,mylist):
  found=False
  
  for i in range(len(mylist)):
    if x==mylist[i]:
      found=True
      break
    else:
      if x not in mylist:
        break

  if found==False:
     print("Not Found")
  else:
    print("Found in position",i)

     

mylist=[1,2,3,4,5]
x=int(input("Enter a number:"))
linearsearch(x,mylist)
  

  'x' is input by the user and is the search criteria

  'int' is the search criteria

  'mylist' is the search criteria in the coded example

  'found' is the input and is the search criteria

 5. In a linear search, each element of the list is checked sequentially (one after the other) starting …
If you are able, watch the video and try and code this yourself in order to fully master it.

  at the end of the list

  in the middle of the list

  at a random point in the list

  at the beginning of the list

 6. Analyse the code below - what will the output be of this code?
def Sequential_Search(dlist, item):

    pos = 0
    found = False
    
    while pos < len(dlist) and not found:
        if dlist[pos] == item:
            found = True
        else:
            pos = pos + 1
    
    return found, pos

print(Sequential_Search([11,23,58,31,56,77,43,12,65,19],12))

  False, 7

  True, 7

  False,12

  True, 12

 7. In the following example (feel free to play around with it) what is "pos"?

  It is a variable indicating the position of the element to be searched, starting at 0 (first element)

  It is a variable that shows the result of the search

  It is a list in itself which shows the position of the elements

  It is a variable that shows how to position the result once found

 8. What is happening on line 10 with the code: pos = pos + 1

  This is called adding one. If the element is found, add one and output result

  This is called incrementation. If the element searched for is not found, increment the value of 'pos'

  This is called incrementation. It is adding one so that numbers aren't too short

  None of the options provided are correct

 9. When is a linear search most useful?

  When the list is very large and can be sorted easily

  When the list is very large and the element searched for is at the end

  When the list is very small or the element searched for is near the start

  When the list is over 256 million elements long

 10. What is one benefit of using a linear search over a binary search?

  In a linear search, the time taken is always four times as quick as a binary search

  In a linear search, the list does not have to be sorted (in order)

  In a linear search, the algorithm puts the list in order which is handy

  None of the options provided are correct