Data Structure Lab :
Practical B14 :
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) Selection Sort
b) Bubble sort and display top five scores.
#author : sppucseguru
nums=[]
n=int(input("Enter Total No. of Students "))
for i in range(n):
ele=int(input("Enter Percentage "))
nums.append(ele)
print(nums)
n=len(nums)
#Bubble Sort
def Bubble_Sort():
swap=0
for i in range(n-1):
for j in range(n-i-1):
if nums[j]>nums[j+1]:
nums[j],nums[j+1]=nums[j+1],nums[j]
swap=1
if(swap==0):
break
#Selection Sort
def Selection_Sort():
for i in range(n-2):
min=i
for j in range(i+1,n-1):
if nums[j]<nums[min]:
min=j
if nums[min]<nums[i]:
nums[i],nums[min]=nums[min],nums[i]
print("Selection_Sort",nums)
def Display():
if(n>5):
print(nums[:5])
else:
print(nums)
while True:
res=input("""Enter algorithm you want to use for sorting
a. Bubble Sort
b. Selection Sort\n""")
if(res=='a'):
Bubble_Sort()
Display()
break
elif(res=='b'):
Selection_Sort()
Display()
break
else:
print("Wrong Respond")
Comments
Post a Comment