English

A list contains roll_no, name and marks of the student. Sort the list in descending order of marks using Selection Sort algorithm.

Advertisements
Advertisements

Question

A list contains roll_no, name and marks of the student. Sort the list in descending order of marks using Selection Sort algorithm.

Code Writing
Advertisements

Solution

Each record is stored as [roll_no, name, marks]. For descending order, selection sort selects the largest marks from the unsorted portion.

def selection_sort_marks(students):
    for i in range(len(students) - 1):
        maximum_index = i

        # Find record with highest marks
        for j in range(i + 1, len(students)):
            if students[j][2] > students[maximum_index][2]:
                maximum_index = j

        # Swap complete student records
        students[i], students[maximum_index] = (
            students[maximum_index], students[i]
        )

    return students


students = [
    [101, "Aman", 78],
    [102, "Beena", 92],
    [103, "Charu", 85]
]

print(selection_sort_marks(students))

[[102, 'Beena', 92], [103, 'Charu', 85], [101, 'Aman', 78]]
shaalaa.com
  Is there an error in this question or solution?
Chapter 5: Liner List Manipulation - EXERCISE [Page 105]

APPEARS IN

CBSE Computer Science with Python [English] Class 12
Chapter 5 Liner List Manipulation
EXERCISE | Q 16. | Page 105
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×