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.
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...
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
PYTHON CODING Using the structural node and methods discussed in Binary Search Tree below # Binary...
PYTHON CODING Using the structural node and methods discussed in Binary Search Tree below # Binary Tree Node structure class Node: # Constructor to create a new node def __init__(self, data): self.data = data self.left = None self.right = None class BSTree(): def __init__(self, rootdata): self.root = Node(rootdata)    def insert(self, data, cur_node): if data < cur_node.data: if cur_node.left == None: cur_node.left = Node(data) else: self.insert(data, cur_node.left)    elif data > cur_node.data: if cur_node.right == None: cur_node.right = Node(data) else:...
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...
Iterative Linear Search, Recursive Binary Search, and Recursive Selection Sort I need it in Java with...
Iterative Linear Search, Recursive Binary Search, and Recursive Selection Sort I need it in Java with comments and I need the input file to be placed with Scanner not BufferedReader Please help I need Class River Class CTRiver and Class Driver Class River describes river’s name and its length in miles. It provides accessor methods (getters) for both variables, toString() method that returns String representation of the river, and method isLong() that returns true if river is above 30 miles...
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT