0% found this document useful (0 votes)
33 views

Daa Ass 1

The document discusses a program that performs various operations like creation, insertion, deletion, searching and sorting on one dimensional arrays. It includes functions to perform each operation and discusses the time complexity of different sorting algorithms like bubble sort, selection sort, insertion sort, merge sort etc.

Uploaded by

mbinqasim71
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

Daa Ass 1

The document discusses a program that performs various operations like creation, insertion, deletion, searching and sorting on one dimensional arrays. It includes functions to perform each operation and discusses the time complexity of different sorting algorithms like bubble sort, selection sort, insertion sort, merge sort etc.

Uploaded by

mbinqasim71
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

BAHRIA UNIVERSITY,

(Karachi Campus)
Department of Software Engineering
Assignment #01– Spring 2023

COURSE TITLE: D&AA COURSE CODE: CSC-321


Class: BSE 4A&C Shift: Morning
Course Instructor: DR.QAMARUDDIN Assignment Date: 05-Mar-2024
Max. Marks: 5 Points Assignment Due: 11-Mar-2024
Presented By: SYED MUHAMMAD BIN QASIM
EN: 02-131222-011

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 linear_search(arr, element):


for i in range(len(arr)):
if arr[i] == element:
return i
return -1

def binary_search(arr, element):


arr.sort()
low, high = 0, len(arr) - 1
while low <= high:
mid = (low + high) // 2
if arr[mid] == element:
return mid
elif arr[mid] < element:
low = mid + 1
else:
high = mid - 1
return -1

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

if result != -1: n+1


print("Element found at index:", result) n
else: nx(n+1)
print("Element not found")

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

while i < len(left_half) and j < len(right_half):


if left_half[i] < right_half[j]:
arr[k] = left_half[i]
i += 1
else:
arr[k] = right_half[j]
j += 1
k += 1
while i < len(left_half):
arr[k] = left_half[i]
i += 1
k += 1

while j < len(right_half):


arr[k] = right_half[j]
j += 1
k += 1

1
def counting_sort(arr):
1
max_value = max(arr)
min_value = min(arr) 1

range_of_elements = max_value - min_value + 1


n+1
count = [0] * range_of_elements
output = [0] * len(arr) n

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 quick_sort(arr, low, high):


1
if low < high:
1
pivot_index = partition(arr, low, high)
(log n)
quick_sort(arr, low, pivot_index - 1)
 (log n)
quick_sort(arr, pivot_index + 1, high)
 (log n)

def partition(arr, low, high):


pivot = arr[high] (log n)
i = low - 1  (log n)
for j in range(low, high):
 (log n)
if arr[j] <= pivot:
 (log n)
i += 1
arr[i], arr[j] = arr[j], arr[i]
arr[i + 1], arr[high] = arr[high], arr[i + 1]
return i + 1

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")

choice = int(input("Enter your choice (1-6): "))

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”)

Question # 02: [CLO3:03]


Derive the time complexity of each algorithm of above problem; also discuss its best, average
and worst case.
1. Bubble Sort:
 Time Complexity:
 Best Case: O(n)
Occurs when the array is already sorted.
 Average Case: O(n^2)
Involves nested loops.
 Worst Case: O(n^2)
Occurs when the array is sorted in reverse order.
2. Selection Sort:
 Time Complexity:
 Best Case: O(n^2)
Involves nested loops.
 Average Case: O(n^2)
Involves nested loops.
 Worst Case: O(n^2)
Involves nested loops.
3. Insertion Sort:
 Time Complexity:
 Best Case: O(n)
Occurs when the array is already sorted.
 Average Case: O(n^2)
Involves nested loops.
 Worst Case: O(n^2)
Occurs when the array is sorted in reverse order.
4. Merge Sort:
 Time Complexity:
 Best Case: O(n log n)
Consistent as it always divides the array in half.
 Average Case: O(n log n)
Consistent.
 Worst Case: O(n log n)
Consistent.
5. Counting Sort:
 Time Complexity:
 Best Case: O(n + k)
Where k is the range of the input.
 Average Case: O(n + k)
Where k is the range of the input.
 Worst Case: O(n + k)
Where k is the range of the input.
6. Quicksort:
 Time Complexity:
 Best Case: O(n log n)
Occurs when the pivot consistently divides the array in half.
 Average Case: O(n log n)
On average, quicksort performs well.
 Worst Case: O(n^2)
Occurs when the pivot is always the smallest or largest element.

You might also like