SE Computer Engineering Practical 20 solution

Home » All Blogs » Python » Python assignments » SE Computer Engineering » SE Computer Engineering Practical 20 solution

SE Computer Engineering Practical 20 solution

Question:

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

Code:

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

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

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

   i = n - 1
   while i >= 0:
       index = int(arr[i] // exp) % 10
       output[count[index] - 1] = arr[i]
       count[index] -= 1
       i -= 1

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




def radix_sort(arr):
   max_value = max(arr)
   exp = 1

   while max_value // exp > 0:
       counting_sort(arr, exp)
       exp *= 10
   print('After Radix Sort :', arr)


def display_top_scores(arr, n):
   print("Top", n, "scores:")
   for i in range(len(arr) - 1, len(arr) - n - 1, -1):
       print(arr[i])


# Store 10th class percentages of students in an array
percentages = [89.5, 56.0, 85.75, 76.2, 93.5, 87.8, 60.25, 48.0, 94.1, 80.9]
print('Before Radix Sort:', percentages)


# Sort the array using radix sort
radix_sort(percentages)

# Display the top five scores
display_top_scores(percentages, 5)

Output:

Before Radix Sort: [89.5, 56.0, 85.75, 76.2, 93.5, 87.8, 60.25, 48.0, 94.1, 80.9]
After Radix Sort : [48.0, 56.0, 60.25, 76.2, 80.9, 85.75, 87.8, 89.5, 93.5, 94.1]
Top 5 scores:
94.1
93.5
89.5
87.8
85.75

Process finished with exit code 0
Tech Amplifier Final Logo