You are on page 1of 8

EX NO:10 SIMPLE SORTING TECHNIQUES

DATE: a) BUBBLE SORT


AIM:
To write a python program to perform bubble sort.
ALGORITHM:
Step 1: Start the program.
Step 2: Call the bubbleSort function by passing unsorted list as argument.
Step 3: Using for loop iterate through the list starting at index 0 till end of list.
Step 4: Compare the current element with the next element in the unsorted list.
Step 5: If the current element is greater than next element, then swap.
Step 6: Repeat this process until all iterations are over.
Step 7: In each iteration one largest element is found out from the unsorted list and
kept in its place.
Step 8: Display the sorted list.
Step 9: Stop the program.
PROGRAM:
def bubbleSort(array):
for i in range(len(array)):
for j in range(0, len(array) - i - 1):
if array[j] > array[j + 1]:
temp = array[j]
array[j] = array[j+1]
array[j+1] = temp
print('**********>> Bubble Sorting <<**********')
lst = []
total_elements = int(input("Enter total number of elements:"))
for i in range(0,total_elements):
a = int(input("Enter an element:"))
lst.append(a)
print("The List before sorting: ")
print(lst)
bubbleSort(lst)
print("The List after sorting: ")
print(lst)
OUTPUT:
**********>> Bubble Sorting <<**********
Enter total number of elements:6
Enter an element:5
Enter an element:1
Enter an element:3
Enter an element:9
Enter an element:2
Enter an element:4
The List before sorting:
[5, 1, 3, 9, 2, 4]
The List after sorting:
[1, 2, 3, 4, 5, 9]

RESULT:
Thus, the above program was executed successfully and the output was
verified.
EX NO:10 SIMPLE SORTING TECHNIQUES
DATE: b) SELECTION SORT
AIM:
To write a python program to perform selection sort.
ALGORITHM:
Step 1: Start the program.
Step 2: Call the selectionSort function by passing unsorted list and its size as
arguments.
Step 3: Initialize min_index to location 0.
Step 4: Using for loop traverse the list to find the minimum element.
Step 5: While traversing if any element smaller than min_index is found then swap
both the values.
Step 6: The increment the min_index to point next location.
Step 7: Repat the process for all the remaining locations.
Step 8: Print the sorted list.
PROGRAM:
def selectionSort(list, size):
for index in range(size):
min_index = index
for j in range(index + 1, size):
if list[j] < list[min_index]:
min_index = j
(list[index], list[min_index]) = (list[min_index], list[index])

print('**********>> Selection Sorting <<**********')


list = [] #empty list created
total_elements = int(input("Enter total number of elements:"))
for i in range(0,total_elements):
a = int(input("Enter an element:"))
list.append(a) #cdding elements to list
print('The List before sorting:')
print(list)
size = len(list)
selectionSort(list, size)
print('The List after sorted by Selection sort:')
print(list)
OUTPUT:
**********>> Selection Sorting <<**********
Enter total number of elements:6
Enter an element:23
Enter an element:78
Enter an element:45
Enter an element:8
Enter an element:32
Enter an element:56
The List before sorting:
[23, 78, 45, 8, 32, 56]
The List after sorted by Selection sort:
[8, 23, 32, 45, 56, 78]

RESULT:
Thus, the above program was executed successfully and the output was
verified.
EX NO:10 SIMPLE SORTING TECHNIQUES
DATE: c) INSERTION SORT
AIM:
To write a python program to perform insertion sort.
ALGORITHM:
Step 1: Start the program.
Step 2: Call the insertionSort function by passing unsorted list as argument.
Step 3: Iterate from A[1] to A[N] of the list.
Step 4: Compare the current element(key) to its predecessor.
Step 5: If the key element is smaller than its predecessor,
then, compare it to the elements before.
Step 6: Move the greater elements one position up to make space for the smaller
element.
Step 7: Repeat this process till the end of list.
Step 8: Print the sorted list.
Step 9: Stop the program.
PROGRAM:
def insertionSort(A):
for i in range(1,len(A)):
currentvalue=A[i]
position = i
while position>0 and A[position-1]>currentvalue:
A[position]=A[position-1]
position = position-1
A[position]= currentvalue
print('**********>> Insertion Sorting <<**********')
X=[]
n=int(input("Enter list size:"))
print('Enter numbers')
for i in range(0,n):
a = int(input("Enter a Number:"))
X.append(a)
print('List before sorting:')
print(X)
insertionSort(X)
print('List after sorting:')
print(X)
OUTPUT:
**********>> Insertion Sorting <<**********
Enter list size:6
Enter numbers
Enter a Number:8
Enter a Number:5
Enter a Number:7
Enter a Number:1
Enter a Number:9
Enter a Number:3
List before sorting:
[8, 5, 7, 1, 9, 3]
List after sorting:
[1, 3, 5, 7, 8, 9]

RESULT:
Thus, the above program was executed successfully and the output was
verified.
EX NO:10 SIMPLE SORTING TECHNIQUES
DATE: d) MERGE SORT
AIM:
To write a python program to perform Merge sort.
ALGORITHM:
Step 1: Start the program.
Step 2: Call the mergeSort function by passing unsorted list as argument.
Step 3: Find the mid of the list.
Step 4: Assign sub_array1 = arr[:mid] and sub_array2 = arr[mid:]
Step 5: Divide the list further by performing recursive call for
mergeSort(sub_array1) and
mergeSort(sub_array2)
repeat the recursive call until sub_array1 and sub_array2 has one element.
Step 6: Initialise i=j=k=0
Step 7: Merge sub_array1 and sub_array2 into arr by sorting the elements in it.
Step 8: Repeat this until all the sublist are merged into one list.
Step 9: Print the sorted list.
Step 10: Stop the program.
PROGRAM:
def mergeSort(arr):
if len(arr) > 1:
mid = len(arr)//2
sub_array1 = arr[:mid]
sub_array2 = arr[mid:]
mergeSort(sub_array1)
mergeSort(sub_array2)

i=j=k=0

while i < len(sub_array1) and j < len(sub_array2):


if sub_array1[i] < sub_array2[j]:
arr[k] = sub_array1[i]
i += 1
else:
arr[k] = sub_array2[j]
j += 1
k += 1

while i < len(sub_array1):


arr[k] = sub_array1[i]
i += 1
k += 1

while j < len(sub_array2):


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

print('**********>> Merge Sorting <<**********')


list = []
total_elements = int(input("Enter total number of elements:"))
for i in range(0,total_elements):
a = int(input("Enter an element:"))
list.append(a) #adding elements to list
print('The List before sorting:')
print(list)
mergeSort(list)
print('The List after sorting:')
print(list)

OUTPUT:
**********>> Merge Sorting <<**********
Enter total number of elements:7
Enter an element:38
Enter an element:27
Enter an element:43
Enter an element:3
Enter an element:9
Enter an element:82
Enter an element:10
The List before sorting:
[38, 27, 43, 3, 9, 82, 10]
The List after sorting:
[3, 9, 10, 27, 38, 43, 82]

RESULT:
Thus, the above program was executed successfully and the output was
verified.

You might also like