Loading...
Development

Module 103

QUICK SORT vs SHELL SORT – Full Comparison with Same Array

Let’s run both algorithms on the exact same array and see step-by-step how they behave.

Array: 45, 12, 78, 23, 56, 9, 67, 34, 89, 41

1. QUICK SORT (Pivot = Last Element)

| Step | Pivot | Partition Result (≤ pivot | pivot | > pivot) | Recursive Calls | |------|-------|--------------------------------------------------|-----------------| | 1 | 41 | 12, 23, 9, 34 | 41 | 45, 78, 56, 67, 89 | | 2 | 34 | 12, 23, 9 | 34 | — (right empty) | | 3 | 9 | — (left empty) | 9 | 12, 23 | | 4 | 23 | 12 | 23 | — | | 5 | 89 | 45, 78, 56, 67 | 89 | — | | 6 | 67 | 45, 56 | 67 | 78 | | 7 | 56 | 45 | 56 | — |

Final Sorted Array: 9, 12, 23, 34, 41, 45, 56, 67, 78, 89

Total Swaps in Partitioning: ~18–22 swaps (depends on exact implementation)

2. SHELL SORT (Gaps 5 → 2 → 1) – Recap

PassGapArray After PassTotal Swaps
159, 12, 34, 23, 41, 45, 67, 78, 89, 565 swaps
229, 12, 34, 23, 41, 45, 67, 56, 89, 781 swap
319, 12, 23, 34, 41, 45, 56, 67, 78, 893 swaps
9 swaps

HEAD-TO-HEAD COMPARISON TABLE

FeatureQuick SortShell SortWinner
Total Swaps (on this array)~20 swapsOnly 9 swapsShell Sort
Comparisons~45–55~60–70Quick Sort
Worst-case TimeO(n²)O(n¹·⁵) or betterShell Sort
Average TimeO(n log n)O(n¹·³) – O(n¹·⁵)Quick Sort
Best-case TimeO(n log n)Nearly O(n) if already groupedShell Sort
Space ComplexityO(log n) recursion stackO(1) – fully in-placeShell Sort
StabilityNot stableNot stableTie
Cache PerformancePoor (random access)Excellent (sequential access)Shell Sort
Performance on Nearly SortedO(n²) if bad pivotsVery fast (few swaps)Shell Sort
Performance on Random DataExtremely fast (fastest in practice)Good, but slower than QuickQuick Sort
Recursive?YesNoShell Sort
Online (can sort as data comes)NoYesShell Sort

Visual Evolution Side-by-Side

After First Major StepQuick Sort (after 1st partition)Shell Sort (after gap=5)
Array12 23 9 34 41 45 78 56 67 899 12 34 23 41 45 67 78 89 56
How "ordered" it looksVery mixedAlready small numbers moved left

Real-World Verdict (2025 Knowledge)

ScenarioBest ChoiceReason
General-purpose sorting (libraries)Quick Sort (or Introsort)Fastest average case
Embedded systems / low memoryShell SortNo recursion, O(1) space
Nearly sorted or partially sortedShell SortAlmost O(n)
Huge arrays (cache matters)Shell SortSequential access
Need guaranteed O(n log n)Merge/Heap SortQuick can degrade
Competitive coding (random data)Quick SortUsually fastest

Conclusion with this example:

  • Shell Sort did 2× fewer swaps than Quick Sort
  • Shell Sort used zero extra space, Quick Sort used recursion stack
  • Shell Sort was more predictable and cache-friendly

Quick Sort wins on average random data
Shell Sort wins on this array and in memory-constrained or nearly-sorted cases

Perfect for exam answer:
"Although Quick Sort has better average time complexity, Shell Sort performed significantly better on this input with only 9 swaps vs ~20, and used no extra space or recursion."

You’re now 100% ready to compare them in viva or theory paper!