DSA, Database System & Operating System — Sorting, Searching, and Graphs, NEC licence examination syllabus (Nepal Engineering Council).
The same "insert into place" idea — but starting with big jumps before settling into small ones.
Shell sort compares and swaps elements that are far apart (using a "gap" sequence, usually starting large and shrinking), progressively reducing the gap down to 1 (at which point it behaves exactly like a final insertion sort pass). Moving elements long distances early avoids the slow, one-step-at-a-time shifting that plain insertion sort suffers from on badly out-of-order data.
Insertion sort's weakness is precise: an element that belongs at the front but sits at the back must be moved one position at a time. Moving it 200 places costs 200 shifts.
Shell sort's insight is that a single comparison across a large gap can accomplish what dozens of adjacent swaps would. Early passes with a big gap get badly-placed elements roughly where they belong cheaply; later passes with smaller gaps refine. By the time the gap reaches 1 — a plain insertion sort — the array is nearly sorted, which is the exact case where insertion sort is at its fastest.Uniquely among the standard sorts, shell sort's performance depends on the gap sequence chosen, and analysing it exactly is an unsolved problem in the general case. Halving the gap each time is the simplest choice but not the best known; better sequences give measurably better worst cases.
It sits between the quadratic sorts and the n log n ones — clearly faster than plain insertion sort on disordered data, without matching merge or heap sort on large inputs. It is also in-place and needs no extra array, which is why it still appears in memory-constrained settings.
Create a free account to tick topics off, take notes as you read, watch the video lessons and get a day-by-day study plan built around your exam date.
Loading…