Sorting Algorithms
A sorting algorithm is a method or process used to arrange elements in a particular order, typically in numerical or lexicographical order. Sorting is a fundamental operation in computer science and is used to organize data for more efficient searching, retrieval, and manipulation.
Example: Selection Sort
How it works:
Find the smallest card. Swap it with the first item.
Find the second-smallest card. Swap it with the second item.
Find the third-smallest card. Swap it with the third item.
Repeat finding the next-smallest card, and swapping it into the correct position until the array is sorted.
Python Code for Selection Sort:
What sorting algorithm does the Python use?
There is a built-in sorting function called sorted()
and lists have a method called .sort()
Timsort is a hybrid, stable sorting algorithm, derived from merge sort and insertion sort, designed to perform well on many kinds of real-world data. It was implemented by Tim Peters in 2002 for use in the Python programming language.
Timsort is classified to be faster than our example of a selection sort.
Using sorted()
sorted()
Using .sort()
.sort()
Connected Readings
Last updated