Professional Documents
Culture Documents
Ex 9
Ex 9
To sort the students in the class according to their heights for group photo in
descending using quick sort algorithm.
ALGORITHM:
Input: List of students with their heights, start index, end index
Output: Sorted list of students in descending order based on height
3. Example usage:
a. Initialize a list of students with dictionaries containing 'name' and 'height'.
b. Print the list before sorting.
c. Call quick_sort_students_by_height with the list of students, start index 0, and end index
len(students) - 1.
d. Print the list after sorting in descending order based on height.
PROGRAM:
def quick_sort_students_by_height(students, start, end):
if start < end:
# Partition the array and get the pivot index
pivot_index = partition(students, start, end)
# Example usage:
if __name__ == "__main__":
students = [
{'name': 'Alice', 'height': 160},
{'name': 'Bob', 'height': 175},
{'name': 'Charlie', 'height': 165},
{'name': 'David', 'height': 180},
{'name': 'Eve', 'height': 155}
]
print("Before sorting:")
for student in students:
print(student)
quick_sort_students_by_height(students, 0, len(students) - 1)
OUTPUT:
Before sorting:
{'name': 'Alice', 'height': 160}
{'name': 'Bob', 'height': 175}
{'name': 'Charlie', 'height': 165}
{'name': 'David', 'height': 180}
{'name': 'Eve', 'height': 155}