| 1 | Initialize gap | 4 | - | - | [23, 12, 1, 8, 34, 54, 2, 3, 7] | Set gap = floor(9/2) = 4 |
| 2 | Compare and insert | 4 | 4 | 4 | [23, 12, 1, 8, 34, 54, 2, 3, 7] | temp=34, no swap needed |
| 3 | Compare and insert | 4 | 5 | 5 | [23, 12, 1, 8, 34, 54, 2, 3, 7] | temp=54, no swap needed |
| 4 | Compare and insert | 4 | 6 | 6 | [23, 12, 1, 8, 34, 54, 2, 3, 7] | temp=2, swap with arr[2]=1? No, 1<2, no swap |
| 5 | Compare and insert | 4 | 7 | 7 | [23, 12, 1, 8, 34, 54, 2, 3, 7] | temp=3, swap with arr[3]=8? Yes, shift 8 right |
| 6 | Shift | 4 | 7 | 3 | [23, 12, 1, 8, 34, 54, 2, 8, 7] | j=3, compare arr[-1] invalid, insert 3 at j=3 |
| 7 | Compare and insert | 4 | 8 | 8 | [23, 12, 1, 3, 34, 54, 2, 8, 7] | temp=7, swap with arr[4]=34? Yes, shift 34 right |
| 8 | Shift | 4 | 8 | 4 | [23, 12, 1, 3, 34, 54, 2, 8, 34] | j=4, compare arr[0]=23 >7? Yes, shift 23 right |
| 9 | Shift | 4 | 8 | 0 | [23, 12, 1, 3, 23, 54, 2, 8, 34] | j=0, no more left, insert 7 at j=0 |
| 10 | Array after gap=4 pass | 4 | - | - | [7, 12, 1, 3, 23, 54, 2, 8, 34] | Array partially sorted with gap 4 |
| 11 | Reduce gap | 2 | - | - | [7, 12, 1, 3, 23, 54, 2, 8, 34] | Set gap = floor(4/2) = 2 |
| 12 | Compare and insert | 2 | 2 | 2 | [7, 12, 1, 3, 23, 54, 2, 8, 34] | temp=1, swap with arr[0]=7? Yes, shift 7 right |
| 13 | Shift | 2 | 2 | 0 | [7, 12, 7, 3, 23, 54, 2, 8, 34] | j=0, insert 1 at j=0 |
| 14 | Compare and insert | 2 | 3 | 3 | [1, 12, 7, 3, 23, 54, 2, 8, 34] | temp=3, swap with arr[1]=12? Yes, shift 12 right |
| 15 | Shift | 2 | 3 | 1 | [1, 12, 7, 12, 23, 54, 2, 8, 34] | j=1, compare arr[-1] invalid, insert 3 at j=1 |
| 16 | Compare and insert | 2 | 4 | 4 | [1, 3, 7, 12, 23, 54, 2, 8, 34] | temp=23, no swap needed |
| 17 | Compare and insert | 2 | 5 | 5 | [1, 3, 7, 12, 23, 54, 2, 8, 34] | temp=54, no swap needed |
| 18 | Compare and insert | 2 | 6 | 6 | [1, 3, 7, 12, 23, 54, 2, 8, 34] | temp=2, swap with arr[4]=23? Yes, shift 23 right |
| 19 | Shift | 2 | 6 | 4 | [1, 3, 7, 12, 23, 54, 23, 8, 34] | j=4, swap with arr[2]=7? Yes, shift 7 right |
| 20 | Shift | 2 | 6 | 2 | [1, 3, 7, 12, 7, 54, 23, 8, 34] | j=2, swap with arr[0]=1? No, insert 2 at j=2 |
| 21 | Compare and insert | 2 | 7 | 7 | [1, 3, 2, 12, 7, 54, 23, 8, 34] | temp=8, swap with arr[5]=54? Yes, shift 54 right |
| 22 | Shift | 2 | 7 | 5 | [1, 3, 2, 12, 7, 54, 23, 54, 34] | j=5, swap with arr[3]=12? Yes, shift 12 right |
| 23 | Shift | 2 | 7 | 3 | [1, 3, 2, 12, 7, 12, 23, 54, 34] | j=3, swap with arr[1]=3? No, insert 8 at j=3 |
| 24 | Compare and insert | 2 | 8 | 8 | [1, 3, 2, 8, 7, 12, 23, 54, 34] | temp=34, swap with arr[6]=23? No, insert 34 at j=8 |
| 25 | Array after gap=2 pass | 2 | - | - | [1, 3, 2, 8, 7, 12, 23, 54, 34] | Array more sorted with gap 2 |
| 26 | Reduce gap | 1 | - | - | [1, 3, 2, 8, 7, 12, 23, 54, 34] | Set gap = floor(2/2) = 1 |
| 27 | Compare and insert | 1 | 1 | 1 | [1, 3, 2, 8, 7, 12, 23, 54, 34] | temp=3, no swap needed |
| 28 | Compare and insert | 1 | 2 | 2 | [1, 3, 2, 8, 7, 12, 23, 54, 34] | temp=2, swap with arr[1]=3? Yes, shift 3 right |
| 29 | Shift | 1 | 2 | 1 | [1, 3, 3, 8, 7, 12, 23, 54, 34] | j=1, insert 2 at j=1 |
| 30 | Compare and insert | 1 | 3 | 3 | [1, 2, 3, 8, 7, 12, 23, 54, 34] | temp=8, no swap needed |
| 31 | Compare and insert | 1 | 4 | 4 | [1, 2, 3, 8, 7, 12, 23, 54, 34] | temp=7, swap with arr[3]=8? Yes, shift 8 right |
| 32 | Shift | 1 | 4 | 3 | [1, 2, 3, 8, 8, 12, 23, 54, 34] | j=3, insert 7 at j=3 |
| 33 | Compare and insert | 1 | 5 | 5 | [1, 2, 3, 7, 8, 12, 23, 54, 34] | temp=12, no swap needed |
| 34 | Compare and insert | 1 | 6 | 6 | [1, 2, 3, 7, 8, 12, 23, 54, 34] | temp=23, no swap needed |
| 35 | Compare and insert | 1 | 7 | 7 | [1, 2, 3, 7, 8, 12, 23, 54, 34] | temp=54, no swap needed |
| 36 | Compare and insert | 1 | 8 | 8 | [1, 2, 3, 7, 8, 12, 23, 54, 34] | temp=34, swap with arr[7]=54? Yes, shift 54 right |
| 37 | Shift | 1 | 8 | 7 | [1, 2, 3, 7, 8, 12, 23, 54, 54] | j=7, insert 34 at j=7 |
| 38 | Array after gap=1 pass | 1 | - | - | [1, 2, 3, 7, 8, 12, 23, 34, 54] | Array fully sorted |
| 39 | End | 0 | - | - | [1, 2, 3, 7, 8, 12, 23, 34, 54] | gap=0, sorting complete |