Exp-3 1
Exp-3 1
Program Code:
def search(arr, x):
for i in range(len(arr)):
if arr[i] == x:
return i
return -1
arr = [10, 20, 80, 30, 60, 50, 110, 100, 130, 170]
x = 20;
if search(arr,x)== -1:
print("Element not found")
else:
print("Element found at index",search(arr,x))
Screenshot of Output:
Program Code:
def bubble_sort(list1):
for i in range(0,len(list1)-1):
for j in range(len(list1)-1):
if(list1[j]>list1[j+1]):
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
temp = list1[j]
list1[j] = list1[j+1]
list1[j+1] = temp
return list1
list1 = [5, 3, 8, 6, 7, 2]
print("The unsorted list is: ", list1)
print("The sorted list is: ", bubble_sort(list1))
Screenshot of Output:
Program Code:
def binary_search(list1, n):
low = 0
high = len(list1) - 1
mid = 0
while low <= high:
mid = (high + low) // 2
if list1[mid] < n:
low = mid + 1
elif list1[mid] > n:
high = mid - 1
else:
return mid
return -1
Screenshot of Output:
Program Code:
def selection_sort(array):
length = len(array)
for i in range(length-1):
minIndex = i
for j in range(i+1, length):
if array[j]<array[minIndex]:
minIndex = j
array[i], array[minIndex] = array[minIndex], array[i]
return array
array = [21,6,9,33,3]
print("The sorted array is: ", selection_sort(array))
Screenshot of Output: