Daa Ass 1
Daa Ass 1
(Karachi Campus)
Department of Software Engineering
Assignment #01– Spring 2023
Question # 1: [CLO3:02]
Write a program that will execute a operation of one dimensional array (creation, Insertion,
deletion, searching, sorting, transverse) also, every function have different types such as insertion
has three option either it keep number in first, last and middle position, same as deletion
operation, however searching have linear and binary option, In addition sorting have bubble,
selection, insertion, merge, counting and quicksort option.
Program:
def create_array():
size = int(input("Enter the size of the array: "))
return [0] * size
def display_array(arr):
print("Array:", arr)
def insert_element(arr):
element = int(input("Enter the element to insert: "))
position = input("Choose position (first/last/middle): ").lower()
if position == 'first':
arr.insert(0, element)
elif position == 'last':
arr.append(element)
elif position == 'middle':
index = len(arr) // 2
arr.insert(index, element)
else:
print("Invalid position")
def delete_element(arr):
element = int(input("Enter the element to delete: "))
position = input("Choose position (first/last/middle): ").lower()
if position == 'first':
if element in arr:
arr.remove(element)
else:
print("Element not found")
elif position == 'last':
if element in arr:
arr.pop()
else:
print("Element not found")
elif position == 'middle':
index = len(arr) // 2
if element in arr:
arr.pop(index)
else:
print("Element not found")
else:
print("Invalid position")
def search_element(arr):
element = int(input("Enter the element to search: "))
method = input("Choose search method (linear/binary): ").lower()
if method == 'linear':
result = linear_search(arr, element)
elif method == 'binary':
result = binary_search(arr, element)
else:
print("Invalid search method")
return 1
n^2
def bubble_sort(arr): n^2
n = len(arr)
n^2
for i in range(n - 1):
for j in range(0, n - i - 1):
if arr[j] > arr[j + 1]:
arr[j], arr[j + 1] = arr[j + 1], arr[j]
1
def selection_sort(arr):
n = len(arr)
n+1
for i in range(n):
min_index = i
n x(n+1)
for j in range(i + 1, n):
if arr[j] < arr[min_index]: n^2
min_index = j
arr[i], arr[min_index] = arr[min_index], arr[i] n^2
def insertion_sort(arr):
n = len(arr)
for i in range(1, n):
key = arr[i]
1
j=i-1
n+1
while j >= 0 and key < arr[j]:
arr[j + 1] = arr[j] n
j -= 1
n
arr[j + 1] = key
nx(n+1)
def merge_sort(arr):
if len(arr) > 1: n^2
mid = len(arr) // 2 n^2
left_half = arr[:mid] n
right_half = arr[mid:]
merge_sort(left_half)
merge_sort(right_half)
i=j=k=0
1
def counting_sort(arr):
1
max_value = max(arr)
min_value = min(arr) 1
1
for i in range(len(arr)):
n+1+k
count[arr[i] - min_value] += 1
n+k
for i in range(1, len(count)):
n+k
count[i] += count[i - 1]
n+k
i = len(arr) - 1
while i >= 0:
output[count[arr[i] - min_value] - 1] = arr[i]
count[arr[i] - min_value] -= 1
i -= 1
for i in range(len(arr)):
arr[i] = output[i]
def sort_array(arr):
method = input("Choose sorting method (bubble/selection/insertion/merge/counting/quick):
").lower()
if method == 'bubble':
bubble_sort(arr)
elif method == 'selection':
selection_sort(arr)
elif method == 'insertion':
insertion_sort(arr)
elif method == 'merge':
merge_sort(arr)
elif method == 'counting':
counting_sort(arr)
elif method == 'quick':
quick_sort(arr, 0, len(arr) - 1)
else:
print("Invalid sorting method")
def main():
array = create_array()
while True:
print("\nOperations:")
print("1. Display Array")
print("2. Insert Element")
print("3. Delete Element")
print("4. Search Element")
print("5. Sort Array")
print("6. Exit")
if choice == 1:
display_array(array)
elif choice == 2:
insert_element(array)
elif choice == 3:
delete_element(array)
elif choice == 4:
search_element(array)
elif choice == 5:
sort_array(array)
elif choice == 6:
print("Exiting program.")
break
else:
print("Invalid choice”)