Question

In: Computer Science

Draw a single graph in Python showing the performance of both a linear and binary search...

Draw a single graph in Python showing the performance of both a linear and binary search algorithm against the number of elements (100,000). Consider the worst case scenario. In the graph we want to see: labeling of the axes, a legend, a title and the graphs for both linear and binary search, again correctly labeled. Explain what you see. Hint: Use Plot Functions (Plot Chart, series from functions) from the H!de Editor.

Solutions

Expert Solution

#source code:

import timeit
import matplotlib.pyplot as plt
def l_search(list,element):
   check=False
   for i in range(len(list)):
       if(list[i]==10):
           check=True
           break
          
       else:
           pass
   if(check==True):
       return "Found"
   else:
       return "not found"
def b_search(list,left,right,element):
   if(right>=left):
       mid=int(left+(right-left)/2)
       if(list[mid]==element):
           return element
       elif(list[mid]>element):
           return b_search(list,left,mid-1,element)
       else:
           return b_search(list,mid+1,right,element)
   else:
       return "not found"
if __name__=="__main__":
#check time taken binary search worst scenario last elements
   list_number=[i for i in range(1,100000)]
   check_element=99999
   start=timeit.timeit()
   b=b_search(list_number,0,len(list_number)-1,check_element)
   if(b=="not found"):
       print("not found")
   else:
       print("found")
   end=timeit.timeit()
   b_time=end-start
   print(b_time)
#check time taken linear search worst scenario last elements
   start=timeit.timeit()
   print(l_search(list_number,check_element))
   end=timeit.timeit()
   l_time=end-start
   print(l_time)
   plt.subplot(1,2,1)
   plt.title("Binary search")
   plt.xlabel("Element")
   plt.ylabel("Time Taken")
   plt.plot(check_element,b_time,"ro")
   plt.legend()
   plt.subplot(1,2,2)
   plt.title("Linear search")
   plt.xlabel("Element")
   plt.ylabel;("Time Taken")
   plt.plot(check_element,l_time,"ro")
   plt.legend()
   plt.show()
  
  
  
  
  
#output:

#if you have any doubt comment below..


Related Solutions

Draw a single graph in Python showing the performance of both algorithms against the number of...
Draw a single graph in Python showing the performance of both algorithms against the number of elements (100,000). Consider the worst case scenario. In the graph we want to see: labeling of the axes, a legend, a title and the graphs for both linear and binary search, again correctly labeled. Explain what you see.
1. Draw a binary search tree as a single root node holding a string as the...
1. Draw a binary search tree as a single root node holding a string as the data element. Each string inserted into a node in the tree will be for a character in a game. Then, draw a new tree each time you insert a new node into the tree holding a string Insert 4 nodes total, including the root. This means the new nodes will need to be inserted at the correct child to maintain the BST property.
Write a program to show the difference between linear search and binary search. Show the input...
Write a program to show the difference between linear search and binary search. Show the input test data for your program and the output produced by your program which clearly show that binary search is faster than linear search
Write a java program that presents the user with the following menu Linear Search Binary Search...
Write a java program that presents the user with the following menu Linear Search Binary Search The user can choose any of these operations using numbers 1 or 2. Once selected, the operation asks the user for an input file to be searched (the file is provided to you and is named input.txt). If option 1 or 2 is selected, it asks the user for the search string. This is the string that the user wants the program to search...
PYTHON CODING Create a method (sortTraversal) for a Binary Search Tree that prints out the Binary...
PYTHON CODING Create a method (sortTraversal) for a Binary Search Tree that prints out the Binary Search Tree in ascending or deceasing order. The order type is an input to the method and can be "ascending" or "descending". The ascending input would return the node values of the tree beginning with the smallest and ending with the largest, descending returns the opposite. Discuss method's Big-O notation. Add proper and consistent documentation to identify code sections or lines to clearly identify...
PYTHON Search for an element in a 2D sorted matrix using the binary search technique. Your...
PYTHON Search for an element in a 2D sorted matrix using the binary search technique. Your code should run in O(m + n) where m is the number of rows and n is the number of columns The python file should have a function named binarysearch The function should take two arguments. The first argument of the function will be the element to search for. Second argument of the function will be the matrix as a List[List]. The function should...
Draw a graph for linear Scatchard and non-linear schatchard with explanation: Thanks
Draw a graph for linear Scatchard and non-linear schatchard with explanation: Thanks
1. What is the Big-O run-time of linear search and binary search in the best, average...
1. What is the Big-O run-time of linear search and binary search in the best, average and worst cases?       Linear Search                  Binary Search Best: Worst: Average: 2. What is the Big-O runtime of the following linked list methods? addFirst() toString() What is the Big-O runtime of the below Stack method? isSorted(Node n) 3. What is the Big-O runtime of the below methods: Method A: Stack Copy Constructor Option 1 public Stack(Stack<T> original) { if(original == null) { return; }...
Rank the algorithms from slowest to fastest. . Bubble Sort Linear Search Binary Search Shellsort
Rank the algorithms from slowest to fastest. . Bubble Sort Linear Search Binary Search Shellsort
Q1 A- How linear search and binary search work? What is their time complexity? B- What...
Q1 A- How linear search and binary search work? What is their time complexity? B- What is a single linked list? What are the pros and cons of the linked list when comparing to parallel arrays? Q2 A- What is a register? How registers work together with ALU and Control Unit to execute a program? What is the fetch-execute cycle? B- What is the difference to implement a control unit using microprogrammed or hardwired approaches? What is clock cycle? What...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT