Sorting
Properties of sorting
- Comparison / Non Comparison - In non-comparison based sorting, elements are sorted from their intrinsic properties instead.
- In-place/Outplace – Means if extra memory is used while sorting. In-place algorithms have space complexity.
- Online/Offline – A sorting is considered online if it can accept new data while the procedure is ongoing
- Stable/Unstable technique – A sorting is stable if the order of elements with the same value is preserved while sorting.
Common sorting algorithms
All sorting algorithms explained below are comparison-based sorting algorithms.
Bubble sort
Biggest or smallest elements are bubbled to the end or start by swapping each consecutive pair.
Insertion sort
Works by building a sorted portion of the array one element at a time.
Start with the second element: Assume the first element is already sorted. Compare the second element with the first, shifting it into the correct position if needed. Move to the next element: Compare it with the sorted portion, shifting larger elements to the right and inserting it into its correct position. Repeat this process for each element until all are sorted.
Merge sort
The array to be sorted is sorted into two sub arrays, with the length . The two sub-arrays are sorted recursively and then merged.
Quicksort
A divide-and-conquer algorithm. A pivot element is selected. The array is partitioned into 2 sub-arrays:
- elements less than the pivot
- elements greater than the pivot
The sub-arrays are then sorted recursively.
Faster than merge sort. Worst case () occurs when the partition scheme is bad and the list is almost sorted.
Heap sort
Uses a binary heap data structure. Operates in two main phases:
- Heap Construction: The input array is transformed into a max heap, where the largest element is at the root of the heap.
- Sorting: The largest element (root of the heap) is swapped with the last element of the array. The heap size is reduced, and the heap property is restored by re-heapifying the remaining elements. This process is repeated until all elements are sorted.
Slower than quick sort.
Tim sort
Created by Tim Peters. A highly optimized version of merge sort. Used in
Python’s List.sort()
and sorted()
and Java’s Collection.sort()
.
Powersort
Built on top of Tim sort. Optimizes the merge phase of tim sort.
Introsort
Begins with quick sort. In large lists, switches to heap sort. The switch
happens when the recursion depth of quick sort exceeds a level proportional to . Switches to insertion sort, once the partition size
is small enough. Used in C++‘s std::sort()
.
Dual-Pivot Quick sort
Two pivots are selected in each end of the array. Left pivot must be smaller than the right pivot. The dataset is partitioned into 3 parts.
- Less than left pivot
- Greater than or equal to left pivot and less than right pivot
- Greater than right pivot
The partitions are recursively sorted. Used in Java’s Array.sort()
.
Comparison
The table below includes information about various sorting algorithms. The table can be scrolled horizontally.
Property | Online | Stable | Best Time | Average Time | Worst Time | Memory |
---|---|---|---|---|---|---|
Bubble sort | No | Yes | ||||
Selection sort | No | No | ||||
Insertion sort | Yes | Yes | ||||
Shell sort | No | No | ||||
Merge sort | No | Yes | ||||
Quick sort | Yes | No | ||||
Heap sort | No | No | ||||
Tim sort | Yes | Yes | ||||
Power sort | Yes | Yes | ||||
Intro sort | No | No |