Python Forum

Full Version: how to sort a list without .sort() function
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hey there.

I would like to understand how I can sort the strings in a list alphabetically, but without using the sort/sorted function.

For example, I have the following list:

list['d', 'v', 'c', 'a', 'b']

Thanks a lot!
Implement a sorting algorithm - there are quite a few and differ in performance characteristics amongst other things. Why, though?
There are lots of sorting algorithms. Insertion sort is probably the easiest to understand. It is much like sorting cards as you pick them up one by one and place them in order in your playing hand. Bubble sort is also pretty easy to understand. These sorts are not very efficient. The more efficient sorts are also a bit abstract and not intuitively obvious at first glance. Well known sorts like Quick Sort and Heap Sort fall in this category.

You can find examples of all these sorts online. Many with fancy graphics to help demonstrate how the sort works.
To make things more interesting you can try sorting in language which alphabet contains non-ascii letters as well.