The question is:
In a class, the physical education teacher recorded the height of the students.
For a sports meet, he has to create a list of the students according to the heights in ascending order. Then he needs to find the person of a height greater or equal to 58 inches to select for a particular sport.
Note: Use Selection sort to sort the students and Binary search to search the students in that list.
Write the PAC, algorithm, and python code to implement the same.
INPUT:
Enter the number of students N
For each N student
Enter the regno.
Enter the height.
OUTPUT:
List of students’ Reg numbers (sorted in order of height)
Register no of the students having height greater than or equal to 58 inches / NOT FOUND
What I did so far:
D=[]
n = int(input("enter number of students:"))
for i in range(0, n):
regno=input('enter registration number: ')
height=int(input('enter hieght of student: '))
D.append(['regno' , regno, 'height' , height])
D.sort(key=lambda x: x[3])
Now what?
According to me the question is not quite right , because it is asking to perform a binary sort to find heights above or equal to 58 . A binary sort will only work if a person with a height of 58 inches is present , cuz in that case we can precisely get the index of element that contains the height 58 . But if the height 58 is not present i.e. if people with height 59,60,61... are present , binary search will not work. Here is my solution building on your approach:
D=[]
n = int(input("enter number of students:"))
# entering the required data to the lists
for i in range(0, n):
regno=input('enter registration number: ')
height=int(input('enter height of student: '))
D.append(['Regno:',regno,'Height:', height])
# performing selection sort (in ascending order)
for step in range(len(D)):
min_id = step
for i in range(step + 1, len(D)):
if D[i][3] < D[min_id][3]:
min_id = i
(D[step], D[min_id]) = (D[min_id], D[step])
#comparing the heights of the students to find the index of the student whose height is nearest to 58 .
for i in range(len(D)):
if D[i][3]<58:
continue
elif D[i][3] >=58:
index=i
break
# once we get the index of the student whose height is the nearest to 58 , we use list slicing to display the records starting from that index.
print("The list of students whose height greater than or equal to 58 inches:")
print(D[index:])