|
1 | | -<h1 align='center'>Example</h1> |
| 1 | +<h1 align='center'>Insertion - Sort - Algorithm - Example</h1> |
2 | 2 |
|
3 | 3 | ## Problem Statement |
4 | 4 | The goal is to sort an array of integers in non-decreasing order using the **Insertion Sort** algorithm. This sorting algorithm is straightforward, works similarly to the way you might sort playing cards, and is particularly effective for small datasets or nearly sorted data. |
5 | 5 |
|
6 | | -### Insertion Sort Algorithm: Detailed Explanation |
| 6 | +### Insertion Sort Algorithm |
7 | 7 |
|
8 | | -**Insertion Sort** builds the sorted array (or list) one item at a time. It takes each element from the input array and inserts it into its correct position in the already sorted portion of the array. |
| 8 | +**Insertion Sort** is a simple and efficient comparison-based sorting algorithm. It works similarly to how you might sort playing cards in your hands: you start with one card and insert each new card into its correct position in the already sorted portion of the hand. |
9 | 9 |
|
10 | | -Here's how the insertion sort algorithm works step by step: |
| 10 | +The key idea behind **insertion sort** is to maintain a "sorted" portion of the list while inserting new elements one by one into the correct position within that sorted portion. |
11 | 11 |
|
12 | | -1. **Start** with the second element in the array (index 1), considering the first element (index 0) as a sorted sub-array. |
13 | | -2. **Compare** the current element (often called `key`) with the elements in the sorted portion of the array (those to the left of it). |
14 | | -3. **Shift** all the elements in the sorted portion that are greater than the `key` one position to the right. This creates space for the `key` to be inserted. |
15 | | -4. **Insert** the `key` into its correct position in the sorted portion. |
16 | | -5. **Repeat** this process for each element in the array until the entire array is sorted. |
| 12 | +### Steps of the Insertion Sort Algorithm: |
| 13 | + |
| 14 | +1. **Start with the second element** (index 1) of the list, considering the first element as a sorted portion. |
| 15 | +2. **Compare the current element** (key) with the elements in the sorted portion (on its left). |
| 16 | +3. **Shift all larger elements** of the sorted portion one position to the right to make room for the key element. |
| 17 | +4. **Insert the key element** into its correct position in the sorted portion. |
| 18 | +5. **Move to the next element** and repeat the process until the entire list is sorted. |
| 19 | + |
| 20 | +### Example: |
| 21 | + |
| 22 | +Let’s sort the list `[5, 3, 8, 4, 2]` using Insertion Sort: |
| 23 | + |
| 24 | +**Initial list**: `[5, 3, 8, 4, 2]` |
| 25 | + |
| 26 | +- **Step 1**: Start with the second element, `3`. |
| 27 | + - Compare `3` with `5`. Since `3` is smaller than `5`, **shift `5` to the right** and insert `3` in the first position. |
| 28 | + - Updated list: `[3, 5, 8, 4, 2]` |
| 29 | + |
| 30 | +- **Step 2**: Move to the next element, `8`. |
| 31 | + - Compare `8` with `5`. Since `8` is greater than `5`, no shifting is needed, and `8` remains in place. |
| 32 | + - Updated list: `[3, 5, 8, 4, 2]` |
| 33 | + |
| 34 | +- **Step 3**: Move to the next element, `4`. |
| 35 | + - Compare `4` with `8`. Since `4` is smaller, **shift `8` to the right**. |
| 36 | + - Compare `4` with `5`. Since `4` is smaller, **shift `5` to the right**. |
| 37 | + - Insert `4` at the position of `5`. |
| 38 | + - Updated list: `[3, 4, 5, 8, 2]` |
| 39 | + |
| 40 | +- **Step 4**: Move to the next element, `2`. |
| 41 | + - Compare `2` with `8`. Since `2` is smaller, **shift `8` to the right**. |
| 42 | + - Compare `2` with `5`. Since `2` is smaller, **shift `5` to the right**. |
| 43 | + - Compare `2` with `4`. Since `2` is smaller, **shift `4` to the right**. |
| 44 | + - Compare `2` with `3`. Since `2` is smaller, **shift `3` to the right**. |
| 45 | + - Insert `2` at the first position. |
| 46 | + - Updated list: `[2, 3, 4, 5, 8]` |
| 47 | + |
| 48 | +Now the list is sorted: `[2, 3, 4, 5, 8]`. |
| 49 | + |
| 50 | +### Characteristics of Insertion Sort: |
| 51 | + |
| 52 | +- **Time Complexity**: |
| 53 | + - Worst-case: **O(n²)** (when the list is sorted in reverse order). |
| 54 | + - Best-case: **O(n)** (when the list is already sorted). |
| 55 | + - Average-case: **O(n²)**. |
| 56 | + |
| 57 | +- **Space Complexity**: **O(1)** (in-place sorting, so no extra space is required). |
| 58 | + |
| 59 | +- **Stable Sorting**: Yes, Insertion Sort is a stable sorting algorithm, meaning that equal elements retain their relative order. |
| 60 | + |
| 61 | +- **Adaptive**: Insertion Sort is adaptive, meaning it performs well when the input is already partially sorted. |
| 62 | + |
| 63 | +### Advantages: |
| 64 | +- **Efficient for small datasets**: Insertion Sort is very efficient for small or nearly sorted datasets, where its time complexity is closer to **O(n)**. |
| 65 | +- **Simple to implement**: The algorithm is easy to understand and implement. |
| 66 | +- **Adaptive**: Performs well when the list is already partially sorted. |
| 67 | +- **Stable**: Maintains the relative order of equal elements. |
| 68 | + |
| 69 | +### Disadvantages: |
| 70 | +- **Inefficient for large datasets**: Due to its **O(n²)** time complexity, it is inefficient on large datasets compared to other algorithms like Merge Sort or Quick Sort. |
| 71 | +- **Slow for large unsorted data**: The algorithm requires a lot of shifts when the data is completely unsorted. |
| 72 | + |
| 73 | +### Visualization: |
| 74 | + |
| 75 | +For better understanding, you can visualize Insertion Sort as inserting a card from an unsorted deck into a hand that is already sorted, gradually growing the sorted portion from left to right. |
17 | 76 |
|
18 | 77 |
|
19 | 78 | ## Problem Solution |
|
0 commit comments