Sorting Algorithms
Top 10 Classic Sorting Algorithms
Sorting algorithms are among the most fundamental algorithms in Data Structures and Algorithms.
Sorting algorithms can be categorized into internal sorting and external sorting. Internal sorting refers to sorting data records within memory, while external sorting refers to sorting data that is too large to fit entirely in memory at once, requiring access to external storage during the sorting process.
Common internal sorting algorithms include insertion sort, shell sort, selection sort, bubble sort, merge sort, quicksort, heap sort, radix sort, etc.
A summarized illustration:
Regarding Time Complexity
Quadratic time complexity (O(n²)) sorts include simple sorting algorithms such as direct insertion, direct selection, and bubble sort.
Linear-logarithmic time complexity (O(nlog₂n)) sorts include quicksort, heap sort, and merge sort.
O(n^(1+§)) sorting, where § is a constant between 0 and 1, applies to shell sort.
Linear time complexity (O(n)) sorts include radix sort, as well as bucket and bin sorting.
Regarding Stability
Stable sorting algorithms: bubble sort, insertion sort, merge sort, and radix sort.
Unstable sorting algorithms: selection sort, quicksort, shell sort, and heap sort.
Terminology:
- n: Size of the dataset
- k: Number of “buckets”
- In-place: Uses constant memory, no additional memory needed
- Out-place: Requires additional memory
- Stability: After sorting, two equal key values remain in the same order as they were before sorting.
Bubble Sort
C Language:
1 | void bubble_sort(int a[], int n) |
Python:
1 | def bubble_sort(lst): |
Selection Sort
C Language:
1 | void selection_sort(int a[], int n) |
Python:
1 | def selection_sort(lst): |
Insertion Sort
C Language:
1 | void insertion_sort(int a[], int n) |
Python:
1 | def insertion_sort(lst): |