Professional Documents
Culture Documents
DSA Lab Experiments - 7 C), 7 D), 7 E) 7 F) 7 G)
DSA Lab Experiments - 7 C), 7 D), 7 E) 7 F) 7 G)
No : 7 c)
Implementation of Bubble Sort
Date :
Aim:
Write a Python program for the implementation of the Bubble Sort algorithm.
Algorithm:
def bubble_sort(array):
for i in range(len(array)):
for j in range(len(array) - 1 - i):
if array[j] > array[j + 1]:
array[j], array[j + 1] = array[j + 1], array[j]
array = [int(input("Enter element " + str(i + 1) + ": "))
for i in range(int(input("Enter the size of the array: ")))]
bubble_sort(array)
print("Sorted array:", array)
Result:
Thus, the Python program for the implementation of the bubble sort algorithm was
executed and verified successfully.
Ex. No : 7 d)
Implementation of Selection Sort
Date :
Aim:
Write a Python program for the implementation of the selection sort algorithm.
Algorithm:
Program:
def selection_sort(array):
for i in range(len(array)):
min_index = i
for j in range(i + 1, len(array)):
if array[j] < array[min_index]:
min_index = j
array[i], array[min_index] = array[min_index], array[i]
array = [int(input("Enter element " + str(i + 1) + ": "))
for i in range(int(input("Enter the size of the array: ")))]
selection_sort(array)
print("Sorted array:", array)
Result:
Thus, the Python program for the implementation of the selection sort algorithm was
executed and verified successfully.
Ex. No : 7 e)
Implementation of Insertion Sort
Date :
Aim:
Write a Python program for the implementation of the insertion sort algorithm.
Algorithm:
Program:
def insertion_sort(array):
for i in range(1, len(array)):
key = array[i]
j=i-1
while j >= 0 and array[j] > key:
array[j + 1] = array[j]
j -= 1
array[j + 1] = key
array = [int(input("Enter element " + str(i + 1) + ": "))
for i in range(int(input("Enter the size of the array: ")))]
insertion_sort(array)
print("Sorted array:", array)
Result:
Thus, the Python program for the implementation of the insertion sort algorithm was
executed and verified successfully.
Ex. No : 7 f)
Implementation of Merge Sort
Date :
Aim:
Write a Python program for the implementation of the merge sort algorithm.
Algorithm:
Result:
Thus, the Python program for the implementation of the merge sort algorithm was
executed and verified successfully.
Ex. No : 7 g)
Implementation of Quick Sort
Date :
Aim:
Write a Python program for the implementation of the quick sort algorithm.
Algorithm:
Program:
Result:
Thus, the Python program for the implementation of the quick sort algorithm was
executed and verified successfully.