0
0
DSA Javascriptprogramming~10 mins

Shell Sort Algorithm in DSA Javascript - Execution Trace

Choose your learning style9 modes available
Concept Flow - Shell Sort Algorithm
Start with array and initial gap
Compare elements gap apart
Swap if out of order
Reduce gap
Repeat until gap = 1 and array sorted
Done
Shell Sort starts with a big gap between elements, compares and swaps them if needed, then reduces the gap until it becomes 1, finishing with a simple insertion sort.
Execution Sample
DSA Javascript
function shellSort(arr) {
  let n = arr.length;
  for (let gap = Math.floor(n/2); gap > 0; gap = Math.floor(gap/2)) {
    for (let i = gap; i < n; i++) {
      let temp = arr[i];
      let j = i;
      while (j >= gap && arr[j - gap] > temp) {
        arr[j] = arr[j - gap];
        j -= gap;
      }
      arr[j] = temp;
    }
  }
  return arr;
}
This code sorts an array using Shell Sort by reducing the gap and inserting elements in the correct position.
Execution Table
StepOperationGapIndex iIndex jArray StateAction
1Initialize gap4--[23, 12, 1, 8, 34, 54, 2, 3, 7]Set gap = floor(9/2) = 4
2Compare and insert444[23, 12, 1, 8, 34, 54, 2, 3, 7]temp=34, no swap needed
3Compare and insert455[23, 12, 1, 8, 34, 54, 2, 3, 7]temp=54, no swap needed
4Compare and insert466[23, 12, 1, 8, 34, 54, 2, 3, 7]temp=2, swap with arr[2]=1? No, 1<2, no swap
5Compare and insert477[23, 12, 1, 8, 34, 54, 2, 3, 7]temp=3, swap with arr[3]=8? Yes, shift 8 right
6Shift473[23, 12, 1, 8, 34, 54, 2, 8, 7]j=3, compare arr[-1] invalid, insert 3 at j=3
7Compare and insert488[23, 12, 1, 3, 34, 54, 2, 8, 7]temp=7, swap with arr[4]=34? Yes, shift 34 right
8Shift484[23, 12, 1, 3, 34, 54, 2, 8, 34]j=4, compare arr[0]=23 >7? Yes, shift 23 right
9Shift480[23, 12, 1, 3, 23, 54, 2, 8, 34]j=0, no more left, insert 7 at j=0
10Array after gap=4 pass4--[7, 12, 1, 3, 23, 54, 2, 8, 34]Array partially sorted with gap 4
11Reduce gap2--[7, 12, 1, 3, 23, 54, 2, 8, 34]Set gap = floor(4/2) = 2
12Compare and insert222[7, 12, 1, 3, 23, 54, 2, 8, 34]temp=1, swap with arr[0]=7? Yes, shift 7 right
13Shift220[7, 12, 7, 3, 23, 54, 2, 8, 34]j=0, insert 1 at j=0
14Compare and insert233[1, 12, 7, 3, 23, 54, 2, 8, 34]temp=3, swap with arr[1]=12? Yes, shift 12 right
15Shift231[1, 12, 7, 12, 23, 54, 2, 8, 34]j=1, compare arr[-1] invalid, insert 3 at j=1
16Compare and insert244[1, 3, 7, 12, 23, 54, 2, 8, 34]temp=23, no swap needed
17Compare and insert255[1, 3, 7, 12, 23, 54, 2, 8, 34]temp=54, no swap needed
18Compare and insert266[1, 3, 7, 12, 23, 54, 2, 8, 34]temp=2, swap with arr[4]=23? Yes, shift 23 right
19Shift264[1, 3, 7, 12, 23, 54, 23, 8, 34]j=4, swap with arr[2]=7? Yes, shift 7 right
20Shift262[1, 3, 7, 12, 7, 54, 23, 8, 34]j=2, swap with arr[0]=1? No, insert 2 at j=2
21Compare and insert277[1, 3, 2, 12, 7, 54, 23, 8, 34]temp=8, swap with arr[5]=54? Yes, shift 54 right
22Shift275[1, 3, 2, 12, 7, 54, 23, 54, 34]j=5, swap with arr[3]=12? Yes, shift 12 right
23Shift273[1, 3, 2, 12, 7, 12, 23, 54, 34]j=3, swap with arr[1]=3? No, insert 8 at j=3
24Compare and insert288[1, 3, 2, 8, 7, 12, 23, 54, 34]temp=34, swap with arr[6]=23? No, insert 34 at j=8
25Array after gap=2 pass2--[1, 3, 2, 8, 7, 12, 23, 54, 34]Array more sorted with gap 2
26Reduce gap1--[1, 3, 2, 8, 7, 12, 23, 54, 34]Set gap = floor(2/2) = 1
27Compare and insert111[1, 3, 2, 8, 7, 12, 23, 54, 34]temp=3, no swap needed
28Compare and insert122[1, 3, 2, 8, 7, 12, 23, 54, 34]temp=2, swap with arr[1]=3? Yes, shift 3 right
29Shift121[1, 3, 3, 8, 7, 12, 23, 54, 34]j=1, insert 2 at j=1
30Compare and insert133[1, 2, 3, 8, 7, 12, 23, 54, 34]temp=8, no swap needed
31Compare and insert144[1, 2, 3, 8, 7, 12, 23, 54, 34]temp=7, swap with arr[3]=8? Yes, shift 8 right
32Shift143[1, 2, 3, 8, 8, 12, 23, 54, 34]j=3, insert 7 at j=3
33Compare and insert155[1, 2, 3, 7, 8, 12, 23, 54, 34]temp=12, no swap needed
34Compare and insert166[1, 2, 3, 7, 8, 12, 23, 54, 34]temp=23, no swap needed
35Compare and insert177[1, 2, 3, 7, 8, 12, 23, 54, 34]temp=54, no swap needed
36Compare and insert188[1, 2, 3, 7, 8, 12, 23, 54, 34]temp=34, swap with arr[7]=54? Yes, shift 54 right
37Shift187[1, 2, 3, 7, 8, 12, 23, 54, 54]j=7, insert 34 at j=7
38Array after gap=1 pass1--[1, 2, 3, 7, 8, 12, 23, 34, 54]Array fully sorted
39End0--[1, 2, 3, 7, 8, 12, 23, 34, 54]gap=0, sorting complete
💡 gap reduced to 0, array is sorted
Variable Tracker
VariableStartAfter Step 10After Step 25After Step 38Final
gap44210
i-888-
j-variesvariesvaries-
array[23,12,1,8,34,54,2,3,7][7,12,1,3,23,54,2,8,34][1,3,2,8,7,12,23,54,34][1,2,3,7,8,12,23,34,54][1,2,3,7,8,12,23,34,54]
Key Moments - 3 Insights
Why do we start with a large gap and reduce it instead of just doing insertion sort?
Starting with a large gap moves elements far apart closer to their correct position quickly, reducing the total number of shifts needed in the final insertion sort with gap=1. See steps 1-10 where gap=4 partially sorts the array.
Why do we compare elements at index j and j-gap instead of adjacent elements?
Shell Sort compares elements gap apart to sort sublists independently. This allows elements to move faster towards their correct position. See steps 12-25 where gap=2 compares elements two apart.
How do we know when the array is fully sorted?
When the gap reduces to 1 and the insertion sort pass completes without swaps, the array is sorted. This is shown in steps 26-38 where gap=1 finishes sorting.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table at step 10, what is the array state after the first gap=4 pass?
A[7, 12, 1, 3, 23, 54, 2, 8, 34]
B[23, 12, 1, 8, 34, 54, 2, 3, 7]
C[1, 3, 2, 8, 7, 12, 23, 54, 34]
D[1, 2, 3, 7, 8, 12, 23, 34, 54]
💡 Hint
Check the 'Array State' column at step 10 in the execution_table.
At which step does the gap reduce from 2 to 1?
AStep 25
BStep 38
CStep 26
DStep 39
💡 Hint
Look for the 'Reduce gap' operation and gap value change in the execution_table.
If the initial gap was set to 1 instead of floor(n/2), how would the execution_table change?
AIt would have more passes with larger gaps.
BIt would only have the gap=1 pass, similar to insertion sort.
CThe array would be sorted faster.
DThe gap would never reduce.
💡 Hint
Refer to the concept_flow and how gap controls passes.
Concept Snapshot
Shell Sort Algorithm:
- Start with gap = floor(n/2)
- Compare and swap elements gap apart
- Reduce gap by half each pass
- Repeat until gap = 1
- Final pass is insertion sort
- Efficient for medium-sized arrays
Full Transcript
Shell Sort works by sorting elements far apart first, then reducing the gap to sort closer elements. We start with a gap of half the array size, compare elements gap apart, and swap if needed. Then we reduce the gap and repeat. When gap is 1, it becomes a simple insertion sort. This method moves elements closer to their correct position faster than insertion sort alone. The execution table shows each step with the array state and pointer positions. Key moments include understanding why gap starts large, why we compare gap-apart elements, and how we know when sorting is done.