Professional Documents
Culture Documents
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])
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
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.