Python Practicals Raj
Python Practicals Raj
array = [2, 4, 0, 1, 9]
x=1
n = len(array)
result = linearSearch(array, n, x)
if(result = = -1):
print("Element not found")
else:
print("Element found at index: ", result)
Output:-
Element found at index: 3
Code:-
def binarySearch(array, x, low, high):
if array[mid] = = x:
return mid
else:
high = mid - 1
return -1
array = [3, 4, 5, 6, 7, 8, 9]
x=4
if result != -1:
print("Element is present at index " + str(result))
else:
print("Not found")
Output:-
Element is present at index 1
Time Complexity :
Best case - O(1)
Average case- O(log n)
Worst case- O(log n)
Output:-
Sorted Array in Ascending Order:
[-9, -2, 0, 11, 45]
Time Complexity :
Best case - O(n2)
Average case- O(n2)
Worst case- O(n2)
data = [9, 5, 1, 4, 3]
insertionSort(data)
print('Sorted Array in Ascending Order:')
print(data)
Output:-
Sorted Array in Ascending Order:
[1, 3, 4, 5, 9]
Time Complexity :
Best case - O(n)
Average case- O(n2)
Worst case- O(n2)
r = len(array)//2
L = array[:r]
M = array[r:]
mergeSort(L)
mergeSort(M)
i=j=k=0
def printList(array):
for i in range(len(array)):
print(array[i], end=" ")
print()
# Driver program
if __name__ = = '__main__':
array = [6, 5, 12, 10, 9, 1]
mergeSort(array)
Output:-
Sorted array is:
1 5 6 9 10 12
Time Complexity :
Best case - O(n*log n)
Average case- O(n*log n)
Worst case- O(n*log n)