Question 17: 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 quick sort and display top five scores. Download hole Program / Project code, by clicking following link: In if-else construct which part will be executed if condition is ture in Java ? In an if-else construct in Java, the code within the if block will be executed if the condition specified in the if statement is true. If the if condition is false, then the code within the else block (if an else block is provided) will be executed. Programming Code: Following code write in: DS_P17.py # Python Program / Project
def Quick(arr, low, high):
if low < high:
m = partition(arr, low, high)
Quick(arr, low, m-1)
Quick(arr, m+1, high)
def partition(arr, low, high):
pivot = arr[low]
i = low + 1
j = high
flag = False
while(not flag):
while(i<=j and arr[i]<=pivot):
i = i+1
while(i<=j and arr[j]>=pivot):
j = j-1
if(j < i):
flag = True
else:
temp = arr[i]
arr[i]= arr[j]
arr[j] = temp
temp = arr[low]
arr[low] = arr[j]
arr[j] = temp
return j
n = int(input("Enter how many students in First Year: "))
arr = []
i = 0
for i in range(n):
item = float(input("Enter percentage marks: "))
arr.append(item)
print("You have entered following students percentage list: ")
print(arr)
print("The sorted percentage list usning Quick Sort is: ")
Quick(arr, 0, n-1)
print(arr)
print("Top five scores is: ")
for i in range(len(arr)-1, 1, -1):
print(arr[i])
# Thanks For Reading.
Output:
# Python Program / Project def Quick(arr, low, high): if low < high: m = partition(arr, low, high) Quick(arr, low, m-1) Quick(arr, m+1, high) def partition(arr, low, high): pivot = arr[low] i = low + 1 j = high flag = False while(not flag): while(i<=j and arr[i]<=pivot): i = i+1 while(i<=j and arr[j]>=pivot): j = j-1 if(j < i): flag = True else: temp = arr[i] arr[i]= arr[j] arr[j] = temp temp = arr[low] arr[low] = arr[j] arr[j] = temp return j n = int(input("Enter how many students in First Year: ")) arr = [] i = 0 for i in range(n): item = float(input("Enter percentage marks: ")) arr.append(item) print("You have entered following students percentage list: ") print(arr) print("The sorted percentage list usning Quick Sort is: ") Quick(arr, 0, n-1) print(arr) print("Top five scores is: ") for i in range(len(arr)-1, 1, -1): print(arr[i]) # Thanks For Reading.
Output:
0 Comments