0
0
DSA C++programming~10 mins

Shell Sort Algorithm in DSA C++ - Execution Trace

Choose your learning style9 modes available
Concept Flow - Shell Sort Algorithm
Start with array
Choose initial gap = n/2
For each gap
Perform gapped insertion sort
Reduce gap (gap = gap/2)
Repeat until gap = 0
Array sorted
Shell Sort starts with a big gap and sorts elements far apart, then reduces the gap and sorts again, repeating until gap is 0 and array is sorted.
Execution Sample
DSA C++
void shellSort(int arr[], int n) {
  for (int gap = n/2; gap > 0; gap /= 2) {
    for (int i = gap; i < n; i++) {
      int temp = arr[i];
      int j;
      for (j = i; j >= gap && arr[j - gap] > temp; j -= gap) {
        arr[j] = arr[j - gap];
      }
      arr[j] = temp;
    }
  }
}
This code sorts an array using Shell Sort by repeatedly sorting elements at a certain gap and reducing the gap until the array is sorted.
Execution Table
StepOperationGapIndex iCompare arr[j-gap] and tempSwap/ShiftArray State
1Initialize gap5---[23, 12, 1, 8, 34, 54, 2, 3, 7, 4]
2Start i loop55arr[0]=23 > temp=54? NoNo shift[23, 12, 1, 8, 34, 54, 2, 3, 7, 4]
3i=656arr[1]=12 > temp=2? YesShift arr[6]=arr[1]=12[23, 12, 1, 8, 34, 54, 12, 3, 7, 4]
4i=651No more shiftPlace temp=2 at arr[1][23, 2, 1, 8, 34, 54, 12, 3, 7, 4]
5i=757arr[2]=1 > temp=3? NoNo shift[23, 2, 1, 8, 34, 54, 12, 3, 7, 4]
6i=858arr[3]=8 > temp=7? YesShift arr[8]=arr[3]=8[23, 2, 1, 8, 34, 54, 12, 3, 8, 4]
7i=853No more shiftPlace temp=7 at arr[3][23, 2, 1, 7, 34, 54, 12, 3, 8, 4]
8i=959arr[4]=34 > temp=4? YesShift arr[9]=arr[4]=34[23, 2, 1, 7, 34, 54, 12, 3, 8, 34]
9i=954No more shiftPlace temp=4 at arr[4][23, 2, 1, 7, 4, 54, 12, 3, 8, 34]
10Reduce gap2---[23, 2, 1, 7, 4, 54, 12, 3, 8, 34]
11i=222arr[0]=23 > temp=1? YesShift arr[2]=arr[0]=23[23, 2, 23, 7, 4, 54, 12, 3, 8, 34]
12i=220No more shiftPlace temp=1 at arr[0][1, 2, 23, 7, 4, 54, 12, 3, 8, 34]
13i=323arr[1]=2 > temp=7? NoNo shift[1, 2, 23, 7, 4, 54, 12, 3, 8, 34]
14i=424arr[2]=23 > temp=4? YesShift arr[4]=arr[2]=23[1, 2, 23, 7, 23, 54, 12, 3, 8, 34]
15i=422arr[0]=1 > temp=4? NoPlace temp=4 at arr[2][1, 2, 4, 7, 23, 54, 12, 3, 8, 34]
16i=525arr[3]=7 > temp=54? NoNo shift[1, 2, 4, 7, 23, 54, 12, 3, 8, 34]
17i=626arr[4]=23 > temp=12? YesShift arr[6]=arr[4]=23[1, 2, 4, 7, 23, 54, 23, 3, 8, 34]
18i=624arr[2]=4 > temp=12? NoPlace temp=12 at arr[4][1, 2, 4, 7, 12, 54, 23, 3, 8, 34]
19i=727arr[5]=54 > temp=3? YesShift arr[7]=arr[5]=54[1, 2, 4, 7, 12, 54, 23, 54, 8, 34]
20i=725arr[3]=7 > temp=3? YesShift arr[5]=arr[3]=7[1, 2, 4, 7, 12, 7, 23, 54, 8, 34]
21i=723arr[1]=2 > temp=3? NoPlace temp=3 at arr[3][1, 2, 4, 3, 12, 7, 23, 54, 8, 34]
22i=828arr[6]=23 > temp=8? YesShift arr[8]=arr[6]=23[1, 2, 4, 3, 12, 7, 23, 54, 23, 34]
23i=826arr[4]=12 > temp=8? YesShift arr[6]=arr[4]=12[1, 2, 4, 3, 12, 7, 12, 54, 23, 34]
24i=824arr[2]=4 > temp=8? NoPlace temp=8 at arr[4][1, 2, 4, 3, 8, 7, 12, 54, 23, 34]
25i=929arr[7]=54 > temp=34? YesShift arr[9]=arr[7]=54[1, 2, 4, 3, 8, 7, 12, 54, 23, 54]
26i=927arr[5]=7 > temp=34? NoPlace temp=34 at arr[7][1, 2, 4, 3, 8, 7, 12, 34, 23, 54]
27Reduce gap1---[1, 2, 4, 3, 8, 7, 12, 34, 23, 54]
28i=111arr[0]=1 > temp=2? NoNo shift[1, 2, 4, 3, 8, 7, 12, 34, 23, 54]
29i=212arr[1]=2 > temp=4? NoNo shift[1, 2, 4, 3, 8, 7, 12, 34, 23, 54]
30i=313arr[2]=4 > temp=3? YesShift arr[3]=arr[2]=4[1, 2, 4, 4, 8, 7, 12, 34, 23, 54]
31i=312arr[1]=2 > temp=3? NoPlace temp=3 at arr[2][1, 2, 3, 4, 8, 7, 12, 34, 23, 54]
32i=414arr[3]=4 > temp=8? NoNo shift[1, 2, 3, 4, 8, 7, 12, 34, 23, 54]
33i=515arr[4]=8 > temp=7? YesShift arr[5]=arr[4]=8[1, 2, 3, 4, 8, 8, 12, 34, 23, 54]
34i=514arr[3]=4 > temp=7? NoPlace temp=7 at arr[4][1, 2, 3, 4, 7, 8, 12, 34, 23, 54]
35i=616arr[5]=8 > temp=12? NoNo shift[1, 2, 3, 4, 7, 8, 12, 34, 23, 54]
36i=717arr[6]=12 > temp=34? NoNo shift[1, 2, 3, 4, 7, 8, 12, 34, 23, 54]
37i=818arr[7]=34 > temp=23? YesShift arr[8]=arr[7]=34[1, 2, 3, 4, 7, 8, 12, 34, 34, 54]
38i=817arr[6]=12 > temp=23? NoPlace temp=23 at arr[7][1, 2, 3, 4, 7, 8, 12, 23, 34, 54]
39i=919arr[8]=34 > temp=54? NoNo shift[1, 2, 3, 4, 7, 8, 12, 23, 34, 54]
40Reduce gap0---[1, 2, 3, 4, 7, 8, 12, 23, 34, 54]
💡 Gap reduced to 0, sorting complete
Variable Tracker
VariableStartAfter gap=5After gap=2After gap=1Final
gapN/A5210
iN/A5 to 92 to 91 to 9N/A
Array State[23, 12, 1, 8, 34, 54, 2, 3, 7, 4][23, 2, 1, 7, 4, 54, 12, 3, 8, 34][1, 2, 4, 3, 8, 7, 12, 34, 23, 54][1, 2, 3, 4, 7, 8, 12, 23, 34, 54][1, 2, 3, 4, 7, 8, 12, 23, 34, 54]
Key Moments - 3 Insights
Why do we start with a large gap and reduce it instead of sorting normally?
Starting with a large gap moves elements far apart closer to their correct positions quickly, reducing the total number of shifts needed later. This is shown in execution_table rows 1-9 where gap=5 sorts distant elements first.
Why do we use a gapped insertion sort inside the loop?
The gapped insertion sort compares and shifts elements that are gap distance apart, sorting sublists. This is visible in rows 11-26 where elements are shifted by gap=2, not adjacent positions.
When does the sorting finish?
Sorting finishes when gap reduces to 0 (row 40), meaning the array is fully sorted with gap=1 insertion sort completed.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, what is the array state after placing temp=2?
A[2, 23, 1, 8, 34, 54, 12, 3, 7, 4]
B[23, 2, 1, 8, 34, 54, 12, 3, 7, 4]
C[23, 12, 2, 8, 34, 54, 1, 3, 7, 4]
D[23, 12, 1, 8, 34, 54, 2, 3, 7, 4]
💡 Hint
Check the 'Array State' column at step 4 in execution_table
At which step does the gap reduce from 5 to 2?
AStep 10
BStep 11
CStep 9
DStep 27
💡 Hint
Look for 'Reduce gap' operation in execution_table
If the initial gap was set to n/3 instead of n/2, how would the execution_table change?
AThe gap would never reduce to zero
BThe array would be sorted immediately without any shifts
CThe gap values in the table would start smaller, changing the steps and array states accordingly
DThe array state would remain unchanged throughout
💡 Hint
Consider how gap initialization affects the sorting passes shown in execution_table
Concept Snapshot
Shell Sort Algorithm:
- Start with gap = n/2
- Perform gapped insertion sort for each gap
- Reduce gap by half each iteration
- Repeat until gap = 0
- Efficiently moves elements closer to sorted position
- Final pass with gap=1 completes sorting
Full Transcript
Shell Sort sorts an array by starting with a large gap between elements and sorting those elements. Then it reduces the gap and sorts again. This repeats until the gap is zero, meaning the array is fully sorted. The code uses a loop for gap starting at n/2, then a nested loop for insertion sort with that gap. Elements are compared and shifted if needed. The execution table shows each step, the gap, the index, comparisons, shifts, and the array state. Key moments include why large gaps help, how gapped insertion sort works, and when sorting finishes. The visual quiz tests understanding of array states and gap changes. The snapshot summarizes the algorithm steps clearly.