Sorting
Algorithms
Master the fundamental sorting techniques in computer science: Selection Sort, Insertion Sort, and Merge Sort, including their performance analysis using Big-O notation.
What Is Sorting?
Sorting is the process of rearranging elements in a collection into a specific order, typically ascending or descending. It is one of the most fundamental operations in computer science and underpins efficient searching, data analysis, and algorithm design.
Why Sorting Matters
Sorted data enables dramatically faster operations. Binary search runs in \( O(\log n) \) on sorted data versus \( O(n) \) for linear search. Many real-world systems depend on sorted data for performance.
Big-O Notation: Measuring Efficiency
Big-O notation describes how an algorithm's running time grows as the input size \( n \) increases. We focus on the worst-case scenario and ignore constants and lower-order terms.
Selection Sort: Find the Minimum
Selection Sort repeatedly finds the minimum element from the unsorted portion and swaps it into the correct position at the front. After each pass, one more element is in its final place.
Selection Sort: Visual Walkthrough
Insertion Sort: Build As You Go
Insertion Sort builds a sorted subarray from left to right. It takes each unsorted element and inserts it into its correct position within the already-sorted portion by shifting larger elements to the right.
Hold the next item while making room
Holding a card aside makes room to shift neighboring cards. In insertion sort, saving the key before shifting protects the value that will be inserted.Save the key before shifting
Insertion Sort: Step by Step
Merge Sort: Divide and Conquer
Merge Sort is a recursive, divide-and-conquer algorithm. It splits the array in half, recursively sorts each half, then merges the two sorted halves back together.
Merge Sort: Splitting and Merging
Selection vs Insertion Sort
| Selection Sort | Insertion Sort | |
|---|---|---|
| Best Case | \( O(n^2) \) | \( O(n) \) |
| Worst Case | \( O(n^2) \) | \( O(n^2) \) |
| Mechanism | Find min, swap | Shift, insert |
| Adaptive? | No | Yes |
How to Choose a Sorting Algorithm
Worked Example: Selection Sort Code
public static void selectionSort(int[] arr) {
for (int i = 0; i < arr.length - 1; i++) {
int minIdx = i;
for (int j = i + 1; j < arr.length; j++) {
if (arr[j] < arr[minIdx])
minIdx = j;
}
// Swap arr[i] and arr[minIdx]
int temp = arr[i];
arr[i] = arr[minIdx];
arr[minIdx] = temp;
}
}
Worked Example: Insertion Sort Code
public static void insertionSort(int[] arr) {
for (int i = 1; i < arr.length; i++) {
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j]; // shift right
j--;
}
arr[j + 1] = key;
}
}
Worked Example: Merge Sort Code
public static void mergeSort(int[] arr, int lo, int hi) {
if (lo < hi) {
int mid = (lo + hi) / 2;
mergeSort(arr, lo, mid);
mergeSort(arr, mid + 1, hi);
merge(arr, lo, mid, hi);
}
}
private static void merge(int[] arr, int lo, int mid, int hi) {
int[] temp = new int[hi - lo + 1];
int i = lo, j = mid + 1, k = 0;
while (i <= mid && j <= hi)
temp[k++] = arr[i] <= arr[j] ? arr[i++] : arr[j++];
while (i <= mid) temp[k++] = arr[i++];
while (j <= hi) temp[k++] = arr[j++];
// copy temp back into arr
for (int t = 0; t < temp.length; t++)
arr[lo + t] = temp[t];
}
Common Mistakes
Algorithm Comparison Summary
| Selection | Insertion | Merge | |
|---|---|---|---|
| Best Case | \( O(n^2) \) | \( O(n) \) | \( O(n \log n) \) |
| Average Case | \( O(n^2) \) | \( O(n^2) \) | \( O(n \log n) \) |
| Worst Case | \( O(n^2) \) | \( O(n^2) \) | \( O(n \log n) \) |
| Space | \( O(1) \) | \( O(1) \) | \( O(n) \) |
| In-Place? | Yes | Yes | No |
| Stable? | No | Yes | Yes |
| Adaptive? | No | Yes | No |
Question 1 of 5
What is the worst-case time complexity of Selection Sort on an array of \( n \) elements?
Question 2 of 5
Which sorting algorithm has a best-case time complexity of \( O(n) \)?
Question 3 of 5
After one complete pass of the outer loop in Selection Sort on the array [5, 3, 8, 1, 4], what is the resulting array?
Question 4 of 5
Which of the following is a disadvantage of Merge Sort compared to Selection Sort and Insertion Sort?
Question 5 of 5
You need to sort a very large dataset (\( n = 1{,}000{,}000 \)) that is in completely random order. Which algorithm should you choose for the best performance?