```
🔶 INSERTION SORT VISUALIZER
Interactive Animation • KEY • CURRENT • SHIFT • INSERT • Comparisons
```
```
📥 Enter Your Values
⚡ Animation Speed
```
```
```
📊 Live Sorting Animation
🟡 KEY
🔵 CURRENT
🟣 COMPARING
🟢 SORTED
```
📈 Live Statistics
Pass
0
KEY
-
CURRENT
-
Comparisons
0
Shifts
0
Ready to sort your array.
```
```
```
📝 Step-by-Step Explanation
1
Insertion Sort starts with the second element.
The first element is considered already sorted.
```
🧮 Insertion Sort Mathematics
Select the next element as
KEY = A[i]
Compare KEY with elements on its left.
While A[j] > KEY
Shift A[j] one position to the right.
Then A[j + 1] = KEY
KEY = A[i]
Compare KEY with elements on its left.
While A[j] > KEY
Shift A[j] one position to the right.
Then A[j + 1] = KEY
```
for i = 1 to n - 1
KEY = A[i]
j = i - 1
while j >= 0 AND A[j] > KEY
A[j + 1] = A[j]
j = j - 1
A[j + 1] = KEY
```
```
```
```
💡 How Insertion Sort Works
1
The first element is considered sorted.
2
Select the next element and call it
KEY.
3
Compare KEY with elements to its left.
4
If the left element is larger than KEY,
shift it one position to the right.
5
Continue shifting until the correct position
for KEY is found.
6
Insert KEY into its correct position.
```
🔢 Mathematical Example
Array:
[12, 11, 13, 5, 6]
Pass 1:
KEY = 11
12 > 11 → Shift 12
Insert 11
Result: [11, 12, 13, 5, 6]
Pass 2:
KEY = 13
12 < 13 → No shift
Result: [11, 12, 13, 5, 6]
```
Pass 1:
KEY = 11
12 > 11 → Shift 12
Insert 11
Result: [11, 12, 13, 5, 6]
Pass 2:
KEY = 13
12 < 13 → No shift
Result: [11, 12, 13, 5, 6]
```
⏱️ Complexity Analysis
Best Case
O(n)
Already sorted
Average Case
O(n²)
Worst Case
O(n²)
Reverse sorted
Insertion Sort is particularly useful when the array is already nearly sorted because it requires relatively few shifts.
```
```
📌 Important Properties
Stable: Yes
In-place: Yes
Adaptive: Yes
Best Case: O(n)
Worst Case: O(n²)
```
In-place: Yes
Adaptive: Yes
Best Case: O(n)
Worst Case: O(n²)
No comments:
Post a Comment