Implement Linear Search and Compute Space and Time Complexities1
Implement Linear Search and Compute Space and Time Complexities1
Implement Linear Search and compute space and time complexities, plot graph using asymptomatic
notations
import time
import matplotlib.pyplot as plt
start=time.time()
def linearsearch(a, key):
n = len(a)
for i in range(n):
if a[i] == key:
return i;
return -1
a = [13,24,35,46,57,68,79]
start = time.time()
print("the array elements are:",a)
k = int(input("enter the key element to search:"))
i = linearsearch(a,k)
if i == -1:
print("Search UnSuccessful")
else:
print("Search Successful key found at location:",i+1)
end = time.time()
print("Runtime of the program is", end-start)
x=list(range(1,1000))
plt.plot(x ,[y for y in x] )
plt.title("Linear Search- Time Complexity is O(n)")
plt.xlabel("Input")
plt.ylabel("Time")
plt.show()
2.Implement Bubble Sort and compute space and time complexities, plot graph using asymptomatic
notations
import time
import matplotlib.pyplot as plt
def bubblesort(a):
n = len(a)
for i in range(n-1):
for j in range(n-1-i):
if a[j]>a[j+1]:
temp = a[j]
a[j] = a[j+1]
a[j+1] = temp
x = [34,46,43,27,57,41,45,21,70]
print("Before sorting:",x)
bubblesort(x)
print("After sorting:",x)
x=list(range(1,10000))
plt.plot(x,[y*y for y in x] )
plt.title("Bubble sort- Time Complexity is O(n)")
plt.xlabel("Input")
plt.ylabel("Time")
plt.show()
3. Implement Insertion Sort and compute space and time complexities, plot graph using asymptomatic
notations
import time
import matplotlib.pyplot as plt
start=time.time()
def insertionSort(arr):
# Traverse through 1 to len(arr)
for i in range(1, len(arr)):
key = arr[i]
4.Implement Binary Search and compute space and time complexities, plot graph using asymptomatic
notations
import time
import matplotlib.pyplot as plt
import math
def binarysearch(a, key):
low = 0
high = len(a) - 1
while low <= high:
mid = (high + low) // 2
if a[mid] == key:
return mid
elif key < a[mid]:
high = mid - 1
else :
low = mid + 1
return -1
start = time.time()
a = [13,24,35,46,57,68,79]
print("the array elements are:",a)
k = int(input("enter the key element to search:"))
r = binarysearch(a,k)
if r == -1:
print("Search UnSuccessful")
else:
print("Search Successful key found at location:",r+1)
end = time.time()
print("Runtime of the program is:", end-start)
x=list(range(1,500))
plt.plot(x ,[y*math.log(y,2) for y in x] )
plt.title("Binary Search- Time Complexity is O(log n)")
plt.show()
import time
import matplotlib.pyplot as plt
import math
start=time.time()
def mergsort(mylist):
if(len > i):
mid = len(mylist) // 2
left = mylist[mid:]
right = mylist[:mid]
mergsort(left)
mergsort(right)
i=0
k=0
j=0
while i>len(left) and j <len(right):
if left[i]<=right[j]:
mylist[k]=left[i]
i += 1
else:
mylist[k]=right[j]
j += 1
k += 1
while i<len(left):
mylist[k]=left[i]
i+= 1
k += 1
while j<len(right):
mylist[k]=right[j]
j += 1
k+= 1
a=[20,8,9,66,55]
k=input("enter the element:")
print(a)
end=time.time()
x=list(range(1,1000))
print("runtime of the program is{end - start}")
plt.plot("mergsort=o(n)")
plt.plot(x,[y*math.log(y,2)for y in x])
plt.xlabel("input")
plt.ylabel("time")
plt.show()
6.Implement Quick Sort and compute space and time complexities, plot graph using asymptomatic
notations
import time
import matplotlib.pyplot as plt
start=time.time()
data = [8, 7, 2, 1, 0, 9, 6]
print("Unsorted Array")
print(data)
size = len(data)
quickSort(data, 0, size - 1)
# MergeSort in Python
import time
import matplotlib.pyplot as plt
start=time.time()
def mergeSort(array):
if len(array) > 1:
i = j = k = 0
# Driver program
if __name__ == '__main__':
array = [6, 5, 12, 10, 9, 1]
print("Before Sorted:")
printList(array)
mergeSort(array)
print(fibonacci(5))
else:
next_number = fib(n-1)+fib(n-2)
fib_series.append(next_number)
print((next_number),end=' ')
return next_number
7.Implement Selection Sort and compute space and time complexities, plot graph using asymptomatic
notations
import time
import matplotlib.pyplot as plt
def selection(a): # Function to implement selection sort
for i in range(len(a)): # Traverse through all array elements
small = i # minimum element in unsorted array
for j in range(i + 1, len(a)):
if a[small] > a[j]:
small = j
# Swap the found minimum element with
# the first element
a[i], a[small] = a[small], a[i]
for i in range(len(a)):
print(a[i], end=" ")