How to sort a list alphabetically in Python without sort function

There are many built-in functions in Python for sorting a list. But this article covers the topic of sorting a list alphabetically in python without using sort function.
Among the various sorting algorithms, we are using quicksort here.
def quicksort(l): if not l: return [] return (quicksort([x for x in l[1:] if x < l[0]]) + [l[0]] + quicksort([x for x in l[1:] if x >= l[0]])) unsort_list = ['F', 'E', 'Q', 'O', 'P'] print("Original list: ", unsort_list) sort_list = quicksort(unsort_list) print("New list: ", sort_list)
Output:
Original list: ['F', 'E', 'Q', 'O', 'P'] New list: ['E', 'F', 'O', 'P', 'Q']
Subscribe
Login
Please login to comment
0 Discussion