Let us take the following array and apply the Insertion Sort algorithm to sort the array in ascending order.
{
"array": [6,3,8,2,7,4],
"showIndices": true,
"specialIndices": []
}
Pass 1
Element at index=1 is the key.
{
"array": ["key : ",3],
"showIndices": false,
"specialIndices": [],
"emptyIndices": [0]
}
Our Goal: Take element 3
at index=1 and insert it into the sorted portion of array.
{
"array": [6,3,8,2,7,4],
"showIndices": true,
"highlightIndices": [1],
"highlightIndicesGreen": [0],
"specialIndices": [],
"labels": {
"1": "key"
}
}
Shift all elements greater than the key 3
, on the left side of the key, one position to the right.
➜ 6 at index=0 is greater than key 3. Move it to right by one position.
{
"array": [6,6,8,2,7,4],
"showIndices": true,
"highlightIndices": [0, 1],
"highlightIndicesGreen": [0,1],
"specialIndices": [],
"emptyCompIndices": [0]
}
Insert key 3
into the correct position index=0.
{
"array": [3,6,8,2,7,4],
"showIndices": true,
"highlightIndicesGreen": [0,1],
"specialIndices": []
}
Pass 2
Element at index=2 is the key.
{
"array": ["key : ",8],
"showIndices": false,
"specialIndices": [],
"emptyIndices": [0]
}
Our Goal: Take element 8
at index=2 and insert it into the sorted portion of array.
{
"array": [3,6,8,2,7,4],
"showIndices": true,
"highlightIndices": [2],
"highlightIndicesGreen": [0,1],
"specialIndices": [],
"labels": {
"2": "key"
}
}
Shift all elements greater than the key 8
, on the left side of the key, one position to the right.
Insert key 8
into the correct position index=2.
{
"array": [3,6,8,2,7,4],
"showIndices": true,
"highlightIndicesGreen": [0,1,2],
"specialIndices": []
}
Pass 3
Element at index=3 is the key.
{
"array": ["key : ",2],
"showIndices": false,
"specialIndices": [],
"emptyIndices": [0]
}
Our Goal: Take element 2
at index=3 and insert it into the sorted portion of array.
{
"array": [3,6,8,2,7,4],
"showIndices": true,
"highlightIndices": [3],
"highlightIndicesGreen": [0,1,2],
"specialIndices": [],
"labels": {
"3": "key"
}
}
Shift all elements greater than the key 2
, on the left side of the key, one position to the right.
➜ 8 at index=2 is greater than key 2. Move it to right by one position.
{
"array": [3,6,8,8,7,4],
"showIndices": true,
"highlightIndices": [2, 3],
"highlightIndicesGreen": [0,1,2,3],
"specialIndices": [],
"emptyCompIndices": [2]
}
➜ 6 at index=1 is greater than key 2. Move it to right by one position.
{
"array": [3,6,6,8,7,4],
"showIndices": true,
"highlightIndices": [1, 2],
"highlightIndicesGreen": [0,1,2,3],
"specialIndices": [],
"emptyCompIndices": [1]
}
➜ 3 at index=0 is greater than key 2. Move it to right by one position.
{
"array": [3,3,6,8,7,4],
"showIndices": true,
"highlightIndices": [0, 1],
"highlightIndicesGreen": [0,1,2,3],
"specialIndices": [],
"emptyCompIndices": [0]
}
Insert key 2
into the correct position index=0.
{
"array": [2,3,6,8,7,4],
"showIndices": true,
"highlightIndicesGreen": [0,1,2,3],
"specialIndices": []
}
Pass 4
Element at index=4 is the key.
{
"array": ["key : ",7],
"showIndices": false,
"specialIndices": [],
"emptyIndices": [0]
}
Our Goal: Take element 7
at index=4 and insert it into the sorted portion of array.
{
"array": [2,3,6,8,7,4],
"showIndices": true,
"highlightIndices": [4],
"highlightIndicesGreen": [0,1,2,3],
"specialIndices": [],
"labels": {
"4": "key"
}
}
Shift all elements greater than the key 7
, on the left side of the key, one position to the right.
➜ 8 at index=3 is greater than key 7. Move it to right by one position.
{
"array": [2,3,6,8,8,4],
"showIndices": true,
"highlightIndices": [3, 4],
"highlightIndicesGreen": [0,1,2,3,4],
"specialIndices": [],
"emptyCompIndices": [3]
}
Insert key 7
into the correct position index=3.
{
"array": [2,3,6,7,8,4],
"showIndices": true,
"highlightIndicesGreen": [0,1,2,3,4],
"specialIndices": []
}
Pass 5
Element at index=5 is the key.
{
"array": ["key : ",4],
"showIndices": false,
"specialIndices": [],
"emptyIndices": [0]
}
Our Goal: Take element 4
at index=5 and insert it into the sorted portion of array.
{
"array": [2,3,6,7,8,4],
"showIndices": true,
"highlightIndices": [5],
"highlightIndicesGreen": [0,1,2,3,4],
"specialIndices": [],
"labels": {
"5": "key"
}
}
Shift all elements greater than the key 4
, on the left side of the key, one position to the right.
➜ 8 at index=4 is greater than key 4. Move it to right by one position.
{
"array": [2,3,6,7,8,8],
"showIndices": true,
"highlightIndices": [4, 5],
"highlightIndicesGreen": [0,1,2,3,4,5],
"specialIndices": [],
"emptyCompIndices": [4]
}
➜ 7 at index=3 is greater than key 4. Move it to right by one position.
{
"array": [2,3,6,7,7,8],
"showIndices": true,
"highlightIndices": [3, 4],
"highlightIndicesGreen": [0,1,2,3,4,5],
"specialIndices": [],
"emptyCompIndices": [3]
}
➜ 6 at index=2 is greater than key 4. Move it to right by one position.
{
"array": [2,3,6,6,7,8],
"showIndices": true,
"highlightIndices": [2, 3],
"highlightIndicesGreen": [0,1,2,3,4,5],
"specialIndices": [],
"emptyCompIndices": [2]
}
Insert key 4
into the correct position index=2.
{
"array": [2,3,4,6,7,8],
"showIndices": true,
"highlightIndicesGreen": [0,1,2,3,4,5],
"specialIndices": []
}
Array is fully sorted.
{
"array": [2,3,4,6,7,8],
"showIndices": false,
"highlightIndicesGreen": [0,1,2,3,4,5],
"specialIndices": []
}