Understanding Insertion Sort and Bubble Sort
This presentation demonstrates two fundamental sorting algorithms with detailed analysis:
We'll examine pseudocode, time complexity, and C++ implementations using the sample array [7, 3, 8, 1, 5].
Description: Builds the final sorted array one item at a time by inserting each element into its proper position.
1. for i = 1 to length(array) - 1: 2. key = array[i] 3. j = i - 1 4. while j >= 0 and array[j] > key: 5. array[j + 1] = array[j] 6. j = j - 1 7. array[j + 1] = key
for i = 1 to length(array) - 1: key = array[i] j = i - 1 while j >= 0 and array[j] > key: array[j + 1] = array[j] j = j - 1 array[j + 1] = key
Scenario: Array is already sorted
Operations: n-1 comparisons, 0 shifts
Explanation: The while loop condition fails immediately for each element, so only one comparison per element is needed.
Scenario: Random array distribution
Operations: ~n²/4 comparisons and shifts
Explanation: On average, each element needs to be compared with half of the sorted portion.
Scenario: Array is reverse sorted
Operations: n(n-1)/2 comparisons and shifts
Explanation: Each element must be compared with all elements in the sorted portion.
Insertion Sort is an in-place algorithm that uses only a constant amount of extra space (variables: key, i, j).
| Array Size (n) | Best Case | Worst Case |
|---|---|---|
| 10 | 9 | 45 |
| 50 | 49 | 1,225 |
| 100 | 99 | 4,950 |
| 500 | 499 | 124,750 |
| 1,000 | 999 | 499,500 |
1. for i = 1 to length(array) - 1: 2. key = array[i] 3. j = i - 1 4. while j >= 0 and array[j] > key: 5. array[j + 1] = array[j] 6. j = j - 1 7. array[j + 1] = key
#include <iostream> using namespace std; void insertionSort(int array[], int n) { for (int i = 1; i < n; i++) { int key = array[i]; int j = i - 1; while (j >= 0 && array[j] > key) { array[j + 1] = array[j]; j--; } array[j + 1] = key; } }
Description: Repeatedly steps through the list, compares adjacent elements and swaps them if they're in wrong order.
1. for i = 0 to length(array) - 2: 2. swapped = false 3. for j = 0 to length(array) - 2 - i: 4. if array[j] > array[j + 1]: 5. swap array[j] and array[j + 1] 6. swapped = true 7. if not swapped: 8. break
for i = 0 to length(array) - 2: swapped = false for j = 0 to length(array) - 2 - i: if array[j] > array[j + 1]: swap array[j] and array[j + 1] swapped = true if not swapped: break
Scenario: Array is already sorted
Operations: n-1 comparisons, 0 swaps
Explanation: First pass makes no swaps, algorithm terminates early using the optimized version with swapped flag.
Scenario: Random array distribution
Operations: ~n²/2 comparisons and swaps
Explanation: On average, about half of all possible pairs need to be swapped.
Scenario: Array is reverse sorted
Operations: n(n-1)/2 comparisons and swaps
Explanation: Every comparison results in a swap, requiring the maximum number of operations.
Bubble Sort is an in-place algorithm that uses only a constant amount of extra space (variables: i, j, swapped, temp).
| Array Size (n) | Best Case | Worst Case |
|---|---|---|
| 10 | 9 | 45 |
| 50 | 49 | 1,225 |
| 100 | 99 | 4,950 |
| 500 | 499 | 124,750 |
| 1,000 | 999 | 499,500 |
1. for i = 0 to length(array) - 2: 2. swapped = false 3. for j = 0 to length(array) - 2 - i: 4. if array[j] > array[j + 1]: 5. swap array[j] and array[j + 1] 6. swapped = true 7. if not swapped: 8. break
#include <iostream> using namespace std; void bubbleSort(int array[], int n) { for (int i = 0; i < n - 1; i++) { bool swapped = false; for (int j = 0; j < n - 1 - i; j++) { if (array[j] > array[j + 1]) { int temp = array[j]; array[j] = array[j + 1]; array[j + 1] = temp; swapped = true; } } if (!swapped) break; } }
Key Findings: Both algorithms achieve the same result with O(n²) complexity, but Insertion Sort is generally more efficient due to fewer comparisons and data movements. Understanding the pseudocode implementation helps visualize how each algorithm processes data differently, and the C++ conversion demonstrates practical implementation considerations.
Recommendation: For educational purposes, both algorithms provide excellent learning opportunities. For practical applications with small datasets, Insertion Sort is preferable, while larger datasets require more advanced algorithms like Quick Sort or Merge Sort.