InterviewSolution
| 1. |
A list contains rollno, name and marks of the student. Sort the list in descending order of marks using selection sort algorithm. |
|
Answer» def selection_sort(DATA_LIST): for i in range(0, len (DATA_LIST)): min = i for j in range(i + 1, len(DATA_LIST)): if DATA_LIST[j][2] > DATA_LIST[min][2]: min = j # swapping DATA_LIST[min] [0] , DATA_LIST[i] [0] = DATA_LIST[i] [0] , DATA_LIST[mm] [0] DATA_LIST[min][l], DATA_LIST[i][l] = DATA_LI ST [i] [ 1 ], DATA_LI ST [min] [ 1 ] DATA_LIST[min] [2] , DATA_LIST[i] [2] = DATA_LIST[i] [2], DATA_LIST[min] [2] print DATA_LIST maxrange=input(“Enter Number of Students: ”) Students=[] for i in range(maxrange): Details=[] Details. append(input(“Enter roll_no”)) Details.append(raw_input(“Enter name”)) Details. append(inprft(“Enter marks”)) Students.append(Details) print Students selection_sort(Students) |
|