def iterative_linear( arr, n, key_element): for x in range(n): if(arr[x] == key_element): return x return -1 arr = [2, 3, 5, 7, 9, 1, 4, 6, 8, 10] max_size = len(arr) key = 8 result = iterative_linear(arr, max_size - 1, key) if result != -1: print ("The element", key," is found at the index " ,(result), "and in the ", (result+1), "position") else: print ("The element %d is not present in the given array" %(key)) def recursive_binary(arr, first, last, key_element): if first <= last: mid = (first + last) // 2 if arr[mid] == key_element: return mid elif arr[mid] > key_element: return recursive_binary(arr, first, mid - 1, key_element) elif arr[mid] < key_element: return recursive_binary(arr, mid + 1, last, key_element) else: return -1 arr = [20, 40, 60, 80, 100] key = 80 max_size = len(arr) result = recursive_binary(arr, 0, max_size - 1, key) if result != -1: print("The element", key, "is present at index", (result), "and in the position", (result + 1)) else: print("The element is not present in the array") def selectionSort(array, size): for ind in range(size): min_index = ind for j in range(ind + 1, size): # select the minimum element in every iteration if array[j] < array[min_index]: min_index = j # swapping the elements to sort the array (array[ind], array[min_index]) = (array[min_index], array[ind]) arr = [-2, 45, 0, 11, -9,88,-97,-202,747] size = len(arr) selectionSort(arr, size) print('The array after sorting in Ascending Order by selection sort is:') print(arr) def mergeSort(alist): print("Splitting ",alist) if len(alist)>1: mid = len(alist)//2 lefthalf = alist[:mid] righthalf = alist[mid:] mergeSort(lefthalf) mergeSort(righthalf) i=0 j=0 k=0 while i < len(lefthalf) and j < len(righthalf): if lefthalf[i] <= righthalf[j]: alist[k]=lefthalf[i] i=i+1 else: alist[k]=righthalf[j] j=j+1 k=k+1 while i < len(lefthalf): alist[k]=lefthalf[i] i=i+1 k=k+1 while j < len(righthalf): alist[k]=righthalf[j] j=j+1 k=k+1 print("Merging ",alist) alist = [54,26,93,17,77,31,44,55,20] mergeSort(alist) print(alist) def insertionSort(array): for step in range(1, len(array)): key = array[step] j = step - 1 while j >= 0 and key < array[j]: array[j + 1] = array[j] j = j - 1 array[j + 1] = key data = [9, 5, 1, 4, 3] insertionSort(data) print('Sorted Array in Ascending Order:') print(data) def isPrime(n): if(n==1 or n==0): return False for i in range(2,n): if(n%i==0): return False return True N = 100; for i in range(1,N+1): if(isPrime(i)): print(i,end=" ") X = [[12,7,3], [4 ,5,6], [7 ,8,9]] Y = [[5,8,1,2], [6,7,3,0], [4,5,9,1]] result = [[0,0,0,0], [0,0,0,0], [0,0,0,0]] for i in range(len(X)): for j in range(len(Y[0])): for k in range(len(Y)): result[i][j] += X[i][k] * Y[k][j] for r in result: print(r)