Question

In: Computer Science

Time Complexity: Build a table in Excel and record the time complexity taken by linear and...

Time Complexity: Build a table in Excel and record the time complexity taken by linear and binary search to look for an element with the following array sizes: 10, 50, 100, 200, 500, 700, 1000. Compare the performance of both algorithms highlighting the time complexity for both algorithms when run on small input size and when run on large input size.


To accomplish this, load a list with the file data and then create a program that selects at random X amount of elements from the original list (where X is a value that the user selects) and place them in an array, then select at random an element in the original list to search in the sampled array using both algorithms. Measure the time it takes for both algorithms on an array of a given input size.

Solutions

Expert Solution

1) Below code is written in python3 and use online compiler to run it

" https://www.onlinegdb.com/online_python_compiler"

It randomly creates array of sizes [10, 50, 100, 200, 500, 700, 1000] sorts them then randomnly chooses an integer from each array and then time complexity is calculated for both Linear search and Binary search. They are then stored in a csv file for excel called table. Thus now we can compare the time complexity for both algorithms when run on small input size and when run on large input size.

import numpy as np
import time
import random
import csv

def LinearSearch(arr, x):
for i in range(len(arr)):
if arr[i] == x:
return i
return -1
  
def BinarySearch(arr, l, r, x):
   while l <= r:
       mid = int(l + (r - l)/2);
       if (arr[mid] == x):
           return mid
       elif arr[mid] < x:
           l = mid + 1
       else:
           r = mid - 1
   return -1

ls_size = [10, 50, 100, 200, 500, 700, 1000]

time_linear_search = []
time_binary_search = []

for i in range(len(ls_size)):
gen_rand_arr = np.random.randint(1,10000,ls_size[i])
gen_rand_arr.sort()
  
x = random.choice(gen_rand_arr)

start_time = time.time()
result = LinearSearch(gen_rand_arr,x)
end_time = time.time()
time_linear_search.append(end_time- start_time)

start_time = time.time()
result = BinarySearch(gen_rand_arr, 0, len(gen_rand_arr)-1, x)
end_time = time.time()
time_binary_search.append(end_time- start_time)

print(time_linear_search,time_binary_search)

with open('table.csv', 'w') as f:
writer = csv.writer(f)
writer.writerows(zip(time_linear_search, time_binary_search))

2) load a list with the file data and then create a program that selects at random X amount of elements from the original list (where X is a value that the user selects) and place them in an array, then select at random an element in the original list to search in the sampled array using both algorithms. Measure the time it takes for both algorithms on an array of a given input size.

As there is no file here random data is generated with 10000 elements. Then user can enter the value of X (size of array). Here the below program selects X elements from above generated array then a random number is generated to search by both methods and outputs time complexity of the same.

import numpy as np
import time
import random
import csv

def LinearSearch(arr, x):
for i in range(len(arr)):
if arr[i] == x:
return i
return -1
  
def BinarySearch(arr, l, r, x):
   while l <= r:
       mid = int(l + (r - l)/2);
       if (arr[mid] == x):
           return mid
       elif arr[mid] < x:
           l = mid + 1
       else:
           r = mid - 1
   return -1

gen_rand_arr = np.random.randint(1,10000,10000)
# gen_rand_arr
print(type(gen_rand_arr))
X = input("Enter value of X (size of array) less than 10000 : ")
array_search = gen_rand_arr[:int(X)]
x = random.choice(gen_rand_arr)

start_time = time.time()
result = LinearSearch(array_search,x)
end_time = time.time()
time_linear_search = end_time- start_time

start_time = time.time()
result = BinarySearch(array_search, 0, len(array_search)-1, x)
end_time = time.time()
time_binary_search = (end_time- start_time)

print("Time complexity for linear search and binary search are : ",time_linear_search,time_binary_search)


Related Solutions

1. Summarize the efforts you have taken thus far to build an employment record that will...
1. Summarize the efforts you have taken thus far to build an employment record that will lead to job offers after graduation 2. Describe one way you have competitive advantage over another, If you do not have a competitive advantage, descibe actions you can take to obtain one. 3. Describe two ways as a student you can use your status to build you network? 4.Descibe two ways that you can use alliances to obtain a job. How can you information...
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...
Enter the data from the table below into Microsoft Excel and determine the linear correlation coefficient,...
Enter the data from the table below into Microsoft Excel and determine the linear correlation coefficient, R2 for time versus position. Then, calculate y/t and determine the linear correlation coefficient, R2 for time versus y/t. Write in the calculated values in the table below (use two decimal places). Time, t (s) Position, y (cm) Velocity, y/t (cm/s) 0/60 0.00 1/60 2.41 2/60 5.09 3/60 8.08 4/60 11.30 5/60 14.79 6/60 18.56 7/60 22.52 8/60 26.96 9/60 31.56 Determine the linear...
What is the auxiliary space and space complexity for a dynamic programming algorithm with time complexity...
What is the auxiliary space and space complexity for a dynamic programming algorithm with time complexity of O(n^2)? Justify.
Use Simple Linear Regression (excel) on the table below. What is the MAD? Period Month Demand...
Use Simple Linear Regression (excel) on the table below. What is the MAD? Period Month Demand 37 January 7077 38 February 7050 39 March 5430 40 April 5475 41 May 5504 42 June 6246 43 July 6361 44 August 6358 45 September 6379 46 October 6430 47 November 6720 48 December 7107
Determine the Best Fit Linear Regression Equation Using Technology - Excel Question The table shows the...
Determine the Best Fit Linear Regression Equation Using Technology - Excel Question The table shows the age in years and the number of hours slept per day by 24 infants who were less than 1 year old. Use Excel to find the best fit linear regression equation, where age is the explanatory variable. Round the slope and intercept to one decimal place. Age Hours 0.03 16.5 0.05 15.2 0.06 16.2 0.08 15.0 0.11 16.0 0.19 16.0 0.21 15.0 0.26 14.5...
Build a simple linear regression for (1) all 50 states, (2) Eastern Time zone states, (3)...
Build a simple linear regression for (1) all 50 states, (2) Eastern Time zone states, (3) Central Time zone states, (4) Mountain Time zone states, and (5) Pacific, Alaska, and Hawaii Time zone states. Compare your results in all five parts and state your judgements. You may use charts and tables in the comparison. Your answers should have values for the coefficient of determination, AOV table, significance levels, residual plots, and the regression fit with their interpretations. Data source: Kaiser...
Please explain what we mean by Time Complexity and Space Complexity. Please provide a short survey...
Please explain what we mean by Time Complexity and Space Complexity. Please provide a short survey of Complexity classes.
Derive the recurrence for the average time complexity of Quick Sort.
Derive the recurrence for the average time complexity of Quick Sort.
Find the Asymptotic time complexity for each of the functions in the following: #Q2 # to...
Find the Asymptotic time complexity for each of the functions in the following: #Q2 # to denote ayymptotic time complexity use the following notation # O(l) O(m) O(a) O(b) # e.g. traversing through l = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] is O(l) #1 def merge(): l = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] m = [15, 16, 17, 18] lm = [] for l_i in l: lm.append(l_i) for m_i in m:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT