Group B

Q18: Write a Python program to store 10th class percentage of students in an array. Write a function for sorting array of floating point numbers in ascending order using radix sort and display top five scores.

Radix Sort

Solution and implementation for Q18 from Data Structures Laboratory (dsl).

grpB_18.py Download
def counting_sort(arr, exp):
    n = len(arr)
    output = [0] * n
    count = [0] * 10

    for i in range(n):
        index = (arr[i] // exp) % 10
        count[index] += 1

    for i in range(1, 10):
        count[i] += count[i - 1]

    for i in range(n - 1, -1, -1):
        index = (arr[i] // exp) % 10
        output[count[index] - 1] = arr[i]
        count[index] -= 1

    for i in range(n):
        arr[i] = output[i]

def radix_sort(arr):
    max_num = max(arr)
    exp = 1
    while max_num // exp > 0:
        counting_sort(arr, exp)
        exp *= 10

def display_top_five(arr):
    top_five = arr[-5:][::-1]
    print("Top five scores are:")
    for score in top_five:
        print(score)

student_percentages = []
num_students = 10

print("Enter the percentage for 10 students:")
for i in range(num_students):
    percentage = float(input(f"Enter the percentage for student {i + 1}: "))
    student_percentages.append(percentage)

radix_sort(student_percentages)
display_top_five(student_percentages)

Other Questions in Data Structures Laboratory

See All Available Questions
Download