Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Ex.

No : 7 c)
Implementation of Bubble Sort
Date :

Aim:
Write a Python program for the implementation of the Bubble Sort algorithm.

Algorithm:

Step 1: Start the Program.


Step 2: Enter the size of the array.
Step 3: Enter elements one by one based on the size of the array.
Step 4: Initialize an index variable i to keep track of the current position in the array.
Step 5 : While i is less than the length of the array, perform the following steps:
Initialize a nested index variable j to 0
Step 6: While j is less than the length of the array minus i (excluding the current element),
perform the following steps.
i. Compare the current element (array[i]) with the next element (array[i + 1]).
ii. If the current element is greater than the next element, swap the two elements
(array[i], array[i + 1] = array[i + 1], array[i]).
Step 7 : Increment i to move to the next element.
Step 8 : If the entire loop completes without any swaps, it indicates that the array is already
sorted.
Step 9: Terminate the algorithm and return the sorted array.
Step 10: Stop the Program
Program:

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:

Step 1: Start the Program.


Step 2: Enter the size of the array
Step 3: Enter element one by one based on the size of the array
Step 4: Initialize an index variable i to keep track of the current position in the unsorted part
of the array.
Step 5: While i is less than the length of the array, perform the following steps:
a. Initialize a variable min_index to store the index of the currently smallest element.
b. Set min_index to the current index i.
c. Iterate through the unsorted part of the array, starting from the element after the current
index, until the end of the array.
Step 6: For each element in the unsorted part, compare it to the current minimum element
Step 7: If an element is found to be smaller than the current minimum, update min_index to
the index of that element.
Step 8: Swap the element at the current index i with the element at the min_index.
Step 9: Increment i to move to the next element in the unsorted part.
Step 10: After the loop completes, the array is sorted in ascending order.
Step 11: Stop the Program

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:

Step 1: Start the Program.


Step 2: Enter the size of the array.
Step 3: Enter element one by one based on the size of the array.
Step 4: Iterate through the unsorted part of the array, starting from the second element
Step 5: For each element in the unsorted part.
a. Extract the current element as the 'key'.
b. Initialize a variable 'j' to the index of the previous element.
c. While 'j' is not less than -1 and the element at 'j' is greater than the 'key'.
Step 6: Move the element at 'j' to the right by one position.
Step 7: Decrement 'j' to compare with the next element.
Step 8: Insert the 'key' into the array at index 'j + 1'.
Step 9: Repeat the step 4 to step 8, until all elements have been inserted into the sorted part of
the array.
Step 10: Stop the Program

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:

Step 1: Start the Program.


Step 2 : Enter the size of an array
Step 3: Enter the elements one by one based on the size of an array
Step 4: Divide the array into two halves.
Step 5: Sort each half using the same merge sort algorithm.
Step 7: Create an empty list to store the merged elements.
Step 8: Compare the first elements of each half of the array.
Step 9: Append the smaller element to the merged list and remove it from its corresponding
half.
Step 10: Repeat step 9 until one half is empty.
Step 11: Append any remaining elements from the other half to the merged list.
Step 12: Merge the two sorted halves into a single sorted array.
Step 13: Stop the Program
Program:

def merge(left, right):


merged = []
while left and right:
if left[0] <= right[0]:
merged.append(left.pop(0))
else:
merged.append(right.pop(0))
merged.extend(left or right)
return merged
def merge_sort(array):
if len(array) <= 1:
return array
mid = len(array) // 2
left = merge_sort(array[:mid])
right = merge_sort(array[mid:])
return merge(left, right)
array = [int(input("Enter element " + str(i + 1) + ": "))
for i in range(int(input("Enter the size of the array: ")))]
sorted_array = merge_sort(array)
print("Sorted array:", sorted_array)

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:

Step 1: Start the Program.


Step 2 : Enter the size of an array.
Step 3: Enter the element one by one based on the size of an array.
Step 4: Select a pivot element from the array.
Step 5: Partition the array around the pivot element
Step 8: Rearrange the elements in the array such that all elements less than the pivot are to its
left and all elements greater than the pivot are to its right.
Step 9: Recursively sort the two subarrays formed by the partition
Step 10: Repeat steps 4 and 5 for the left subarray until it is sorted.
Step 11: Repeat steps 1 and 2 for the right subarray until it is sorted.
Step 12: Stop the Program

Program:

def partition(array, low, high):


pivot = array[high]
i = low - 1
for j in range(low, high):
if array[j] <= pivot:
i += 1
array[i], array[j] = array[j], array[i]
array[i + 1], array[high] = array[high], array[i + 1]
return i + 1
def quick_sort(array, low, high):
if low < high:
pi = partition(array, low, high)
quick_sort(array, low, pi - 1)
quick_sort(array, pi + 1, high)

array = [int(input("Enter element " + str(i + 1) + ": "))


for i in range(int(input("Enter the size of the array: ")))]
quick_sort(array, 0, len(array) - 1)
print("Sorted array:", array)

Result:

Thus, the Python program for the implementation of the quick sort algorithm was
executed and verified successfully.

You might also like