sorting algorithm
A sorting algorithm is an algorithm that arranges the elements of a sequence into a defined order, most often ascending or descending by a key. Sorting is one of the most studied problems in computing, because ordered data makes many later operations faster and simpler.
Sorting algorithms differ in speed, memory use, and behavior on already-ordered input. Comparison-based methods like quicksort, merge sort, and heapsort decide order by comparing pairs of elements, and they cannot beat O(n log n) time in the worst case. Non-comparison methods like counting sort and radix sort instead exploit the structure of the keys to run faster on suitable data.
These families split the common algorithms two ways:
Two further properties matter in practice. A stable sort preserves the relative order of equal elements, and an in-place sort rearranges the data without allocating a second copy. Ordered output also enables fast lookup through binary search.
Most languages ship a well-tuned general-purpose sort algorithm, so hand-written sorting is rare in application code. Python’s built-in sort uses Timsort, a hybrid of merge sort and insertion sort. Knowing the underlying algorithm still helps in predicting time complexity.
Related Resources
Tutorial
Sorting Algorithms in Python
In this tutorial, you'll learn all about five different sorting algorithms in Python from both a theoretical and a practical standpoint. You'll also learn several related and important concepts, including Big O notation and recursion.
For additional information on related topics, take a look at the following resources:
- Introduction to Sorting Algorithms in Python (Course)
- How to Use sorted() and .sort() in Python (Tutorial)
- pandas Sort: Your Guide to Sorting Data in Python (Tutorial)
- Sorting a Python Dictionary: Values, Keys, and More (Tutorial)
- How to Sort Unicode Strings Alphabetically in Python (Tutorial)
- Sorting Data With Python (Course)
- Sorting Data in Python With pandas (Course)
- How to Use sorted() and .sort() in Python (Quiz)
- Sorting Dictionaries in Python: Keys, Values, and More (Course)
- Sorting a Python Dictionary: Values, Keys, and More (Quiz)