हिंदी

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

प्रश्न

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

कोड लेखन
Advertisements

उत्तर

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
  क्या इस प्रश्न या उत्तर में कोई त्रुटि है?
अध्याय 5: Liner List Manipulation - EXERCISE [पृष्ठ १०५]

APPEARS IN

सीबीएसई Computer Science with Python [English] Class 12
अध्याय 5 Liner List Manipulation
EXERCISE | Q 16. | पृष्ठ १०५
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×