'''
Class: SE-B Computer Engineering
Roll No: ------Your Roll No----------
Subject : Data Structures Lab
Practical No : 05
Title: Write a Python program to store first year percentage of students in array.
Write function for sorting array of floating point numbers in ascending order
using
a) Bubble Sort
b) Selection Sort
b) Insertion Sort and display top five scores.
'''
#......................INPUTS..........................
#..........Size of Array
size = 5
#..........Array for Bubble Sort
Ary1 = [ 5, 4, 3, 2, 1]
#..........Array for Selection Sort
Ary2 = [ 5, 4, 3, 2, 1]
#..........Array for Insertion Sort
Ary3 = [ 5, 4, 3, 2, 1]
#......................FUNCTIONS..........................
def bubble_Sort():
print("\n ................Function : 01 - Bubble Sort....................")
print("\n List Before Sorting: ", Ary1)
#i=j=0
for i in range(0, size-1): #.....for size-1 Passes
for j in range(0, size-1): #.....for size-1 Comparisons in each Passes
if Ary1[ j ] > Ary1[ j+ 1]: #.....if First value is greater than Second
temp = Ary1[ j ] #......Swap the values
Ary1[ j ] = Ary1[ j+1 ]
Ary1[ j+1 ] = temp
print("\n Pass ", i+1, " = ",Ary1) #.....Print each Pass of Sorting.
#.....Function Call
bubble_Sort()
def selection_Sort():
print("\n ................Function : 02 - Selection Sort....................")
print("\n List Before Sorting: ", Ary2)
i=0
for i in range(size-1): #.....for size-1 Passes
j=i+1 #......index j always starts from the
next of i
while j < size: #.....index j will go upto last element
if Ary2[ i ] > Ary2[ j ]: #......if First value is greter than any next value
temp = Ary2[ i ] #......Swap
Ary2[ i ] = Ary2[ j ]
Ary2[ j ] = temp
j += 1
print("\n Pass ", i+1, " = ",Ary2)
#.....Function Call
selection_Sort()
def insertion_Sort():
print("\n ................Function : 03 - Insertion Sort....................")
print("\n List Before Sorting: ", Ary3)
i=1 #......Current = Second Element
while i < size: #.....for size-1 Passes
j=0
while j < i: #.....to Compare with all previous elements of Current
if Ary3[ i ] < Ary3[ j ]: #.....if Next value < Previous value
temp = Ary3[ i ] #....Store Current in a variable
k=i-1 #.....Start Shifting from Previous element of Current
while k >= j: #.....Shifting A[i-1] to A[ i ] until i > j
Ary3[ k + 1 ] = Ary3[ k ]
k -= 1
Ary3[ j ] = temp #....Place Current Value at A[ j ]
j += 1
print("\n Pass ", i, " = ",Ary3)
i += 1
#.....Function Call
insertion_Sort()
'''--------------------------OUTPUT-----------------------------
student@ubuntu:~$ python3 DSLPr-05_BSISort.py
................Function : 01 - Bubble Sort....................
List Before Sorting: [5, 4, 3, 2, 1]
Pass 1 = [4, 3, 2, 1, 5]
Pass 2 = [3, 2, 1, 4, 5]
Pass 3 = [2, 1, 3, 4, 5]
Pass 4 = [1, 2, 3, 4, 5]
................Function : 02 - Selection Sort....................
List Before Sorting: [5, 4, 3, 2, 1]
Pass 1 = [1, 5, 4, 3, 2]
Pass 2 = [1, 2, 5, 4, 3]
Pass 3 = [1, 2, 3, 5, 4]
Pass 4 = [1, 2, 3, 4, 5]
................Function : 03 - Insertion Sort....................
List Before Sorting: [5, 4, 3, 2, 1]
Pass 1 = [4, 5, 3, 2, 1]
Pass 2 = [3, 4, 5, 2, 1]
Pass 3 = [2, 3, 4, 5, 1]
Pass 4 = [1, 2, 3, 4, 5]
student@ubuntu:~$
--------------------------------------------------------------------'''