Question

In: Computer Science

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.

Solutions

Expert Solution


#------- plotting.py -----------

import random
import time
import matplotlib.pyplot as plt

#linear search method that returns the
#index of num in array if not found return -1
def linear_search(array,num):
   for i in range(len(array)):
       if(array[i] == num):
           return i
   return -1
#binary search method that returns the
#index of num in array if not found return -1
def binary_search(array,num):
   low = 0
   high = len(array)-1
   while(low<high):
       mid = low + (high-low)//2
      
       if(array[mid] == num):
           return mid
       elif(array[mid] < num):
           low = mid + 1
       else:
           high = mid - 1
   return -1

#create array of given size
size = 100000
array = [x for x in range(size)]
#worst case scenario for finding is giving a number that is not there.
#in both search methods it will be the worst scenario.

#linear search times
linear_times = []
#binary search times
binary_times = []
row = [x for x in range(10)]
for i in range(10):
   print("\n------- Test -",i+1,"------")
   findOne = random.randint(100000,120000)
   print("\nSearching:",findOne,"using Linear Search")
   #start time for linear search
   st_time = time.time()
   linear_search(array,findOne)
   end_time = time.time() - st_time
   linear_times.append(end_time)
  
   #cal time for binary search
   findTwo = random.randint(-100,-1)
   print("\nSearching:",findTwo,"using Binary Search Search")
   b_st_time = time.time()
   binary_search(array,findTwo)
   b_end_time = time.time() - b_st_time
  
   binary_times.append(b_end_time)

#plot linear times
plt.plot(row,linear_times,"r",label="Linear Search Time")
#plot binary times
plt.plot(row,binary_times,"g",label="Binary Search Time")
#set legends
plt.legend(loc="upper left")
#set axes labels
plt.xlabel("Number of Test runs")
plt.ylabel("Time Taken")
plt.title("Performance of Search Algorithms for Size: " + str(size))
plt.show()

OBSERVATION:

It is clearly observed that linear search takes much more time than the binary search for a given array of size 100000,


Related Solutions

1. Draw a graph of the Solow growth model showing an economy that is in a...
1. Draw a graph of the Solow growth model showing an economy that is in a steady state that has less capital than the golden-rule steady state. Be sure to label both axes correctly as well as the three relevant curves that should appear in the graph. How did you determine graphically where the golden-rule steady-state should be? What would have to happen in this economy for it to reach its golden-rule steady-state?
Draw a typical Total Product (TP) curve in one graph. Right under the graph showing the...
Draw a typical Total Product (TP) curve in one graph. Right under the graph showing the TP curve, draw a typical Average Product and Marginal Product curves in another graph; using the same scales. On the graphs, show the three stages of production. Justify the rationale for dividing the production process into the three stages.
Draw a graph showing the market demand and supply for beef and the demand for beef...
Draw a graph showing the market demand and supply for beef and the demand for beef produced by one beef farmer.  Make sure that you indicate the market price and the price received by the beef farmer.  Assume that the beef market is perfectly competitive.
Draw a correctly labeled graph showing a long-run average cost curve.  Label the sections showing economies of...
Draw a correctly labeled graph showing a long-run average cost curve.  Label the sections showing economies of scale, diseconomies of scale and constant returns to scale.   Explain economies of scale, constant returns to scale, and diseconomies of scale. Multiple Choice In the long run: All inputs can be varied. No inputs can be varied. Only variable inputs can be varied. Only fixed inputs can be varied. A limited number of inputs can be varied. Sunk costs: Are costs that can be recovered...
Draw a graph showing a market and a perfectly competitive firm in long run equilibrium.
Draw a graph showing a market and a perfectly competitive firm in long run equilibrium.
Draw a graph showing the present value of a $1000 payment made 10 year in the...
Draw a graph showing the present value of a $1000 payment made 10 year in the future on the y-axis, and discount rate on the x-axis.
25. Draw a graph showing the short- and long-run effects of an increase in the money...
25. Draw a graph showing the short- and long-run effects of an increase in the money supply.
Draw a graph that shows the demand for a single stadium that holds games for the...
Draw a graph that shows the demand for a single stadium that holds games for the St. Louis Cardinals. Assume that the supply curve for seating is horizontal and then vertical. Further assume that you are graphing the attendance of one particular game on the horizontal axis. Graphically show and verbally explain why the price and quantity might be different after Dexter Fowler (star player) has joined the team than it was before.
Independent trucking is an industry that can be considered perfectly competitive. Draw a graph showing market...
Independent trucking is an industry that can be considered perfectly competitive. Draw a graph showing market supply, market demand, and equilibrium price and quantity. Draw a corresponding graph for the individual firm/trucker using the market equilibrium price and marginal cost curve. If you line up the two graphs horizontally, the equilibrium price should be the same on both graphs. Now suppose that GDP increases as U.S. manufacturers produce more output. What impact will this have on the independent trucking industry...
Independent trucking is an industry that can be considered perfectly competitive. Draw a graph showing market...
Independent trucking is an industry that can be considered perfectly competitive. Draw a graph showing market supply, market demand, and equilibrium price and quantity. Draw a corresponding graph for the individual firm/trucker using the market equilibrium price and marginal cost curve. If you line up the two graphs horizontally, the equilibrium price should be the same on both graphs. Now suppose that GDP increases as U.S. manufacturers produce more output. What impact will this have on the independent trucking industry...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT