Search code examples
pythonarrayslistcsvinsertion-sort

How to sort students list according to their Marks using Insertion sort in Python


I am trying to sort students list and rank each student according to their marks using insertion sort. Data of students include Name, Roll no, Address, Mark.

Here, I store the Mark of students in one list - Marklist and other data of students in a second list - stdData.

I sorted the student Mark List using Insertion sort. But right now I have 2 separate lists. How can I merge and print the sorted list of each student with their marks?

import csv
stdData = []  # store RollNum,student name last name,address
Marklist = []  # store the final mark of each student
#generallist=[]
with open("studentlist.csv", "r") as f1:
    recordReader = csv.DictReader(f1)
    for row in recordReader:
        #generallist.append(row)
        row['Mark']=int(row['Mark'])
        Marklist.append(row['Mark'])
        stdData.append(row['RollNo'])
        stdData.append(row['Name'])
        stdData.append(row['LastName'])
        stdData.append(row['Address'])
print(Marklist)
print(stdData)
for i in range(1, len(Marklist)):
    key = Marklist[i]
    j = i - 1
    while j >= 0 and key < Marklist[j]:
        Marklist[j + 1] = Marklist[j]
        j -= 1
    Marklist[j + 1] = key

   print("Sorted List: ",Marklist)

Thanks.


Solution

  • You are very near to the correct solution. The answer lies in

    • Storing student details as list of list. Eg: [ [student1 details], [student2 details], [student3 details] ]
    • sorting stdData using the indices of MarkList.

    Below is the code modified to address the above points:

    import csv
    stdData = []  # store RollNum,student name last name,address
    Marklist = []  # store the final mark of each student
    generallist=[]
    with open("studentlist.csv", "r") as f1:
        recordReader = csv.DictReader(f1)
        for row in recordReader:
            #generallist.append(row)
            row['Mark']=int(row['Mark'])
            Marklist.append(row['Mark'])
            tmp_data = []
            tmp_data.append(row['RollNo'])
            tmp_data.append(row['Name'])
            tmp_data.append(row['LastName'])
            tmp_data.append(row['Address'])
            stdData.append(tmp_data) # Storing student details as list of lists
    print(Marklist)
    print(stdData)
    for i in range(1, len(Marklist)):
        key = Marklist[i]
        data = stdData[i] # Sort the elements in stdData using indices of MarkList
        j = i - 1
        while j >= 0 and key < Marklist[j]:
            Marklist[j + 1] = Marklist[j]
            stdData[j+1] = stdData[j]
            j -= 1
        Marklist[j + 1] = key
        stdData[j+1] = data
    print("Sorted List: ",Marklist)
    for student_data in stdData:
        print(student_data)
    

    Even though the above solution gives the correct answer, it uses two lists.

    We can sort a list using keys (need not to be actual list elements). The below code implements it and is a better solution.

    import csv
    stdData = []  # store RollNum,student name last name,address
    
    with open("studentlist.csv", "r") as f1:
        recordReader = csv.DictReader(f1)
        for row in recordReader:
            tmp_data = []
            tmp_data.append(row['RollNo'])
            tmp_data.append(row['Name'])
            tmp_data.append(int(row['Mark']))
            tmp_data.append(row['LastName'])
            tmp_data.append(row['Address'])
            stdData.append(tmp_data) # Storing student details as list of lists
    
    print(stdData)
    for i in range(1, len(stdData)):
        key = stdData[i][2] # here the key is the mark
        data = stdData[i] # we will copy the data to correct index
        j = i - 1
        while j >= 0 and key < stdData[j][2]:
            stdData[j+1] = stdData[j]
            j -= 1
        stdData[j+1] = data
    
    print("Sorted List:")
    for rollno, name, mark, lastname, address in stdData:
        print(rollno, name, mark, lastname, address)
    

    Happy coding.