• Home
  • Help
  • Register
  • Login
  • Home
  • Members
  • Help
  • Search

 
  • 0 Vote(s) - 0 Average

Analyze the time complexity of insertion sort

#1
07-13-2022, 05:03 AM
You see insertion sort builds order one element at a time. I recall you asked about its timing before. We both know the outer pass goes across every item. That pass starts from the second spot and moves forward. Each time it grabs one value and looks back. I notice the back checks can stretch out far.
You watch how the inner checks happen next. They compare the grabbed value to prior ones. Shifts happen if something sits in the wrong spot. Those shifts keep going until a proper place opens up. I see this creates different run lengths every pass. The first pass might shift just once. Later passes could shift many times over.
And that pattern leads straight to the count of steps. Worst conditions make every new item move past all earlier ones. You end up with sums that grow like a triangle. One plus two plus three keeps adding until it hits near half of n squared. I get why people call it quadratic then. The inner work doubles roughly with each outer step.
But best conditions change everything fast. When things already sit in order the inner checks stop quick. You barely move anything at all. Only a single compare happens per outer step. That drops the whole thing down to linear speed. Perhaps you spotted this when testing small sets yourself.
Now average cases sit closer to the slow side. Random order means about half the prior items get shifted on average. You still face that quadratic buildup overall. I tried running it on mixed data last week. The time grew steadily with bigger inputs.
Or think about the total compares made. Every pair of elements might get looked at once. This happens because later items can bump into all before them. You end up comparing roughly n times n over two. That explains the n squared label clearly.
Then space stays tiny since nothing extra gets allocated. I like how it works in place without extra room. You just reuse the same array spots. Shifts happen right inside the list. No new structures pop up during the process.
Maybe swap tests show why it lags behind quicker methods. Insertion sort feels simple yet drags when data grows. You see the inner jostling cramps the speed. Early passes finish light but later ones pile on work. I ran some mental tallies and the additions always pointed back to squares.
Also consider small arrays where it shines anyway. The overhead stays low so constants do not bite hard. You gain from skipping fancy setups in tiny jobs. But scale it up and the shifts multiply quick. That quadratic bite shows itself after a few hundred items.
I notice the best case pops up rarely in real feeds. Random inputs push toward the full n squared cost. You measure it by watching how far each insert travels back. Each travel adds compares plus moves. The total moves equal the disorder level in the set.
Perhaps break the loops into parts for counting. Outer runs exactly n minus one times. Inner runs from zero up to the current index. Summing those gives the triangle formula again. You reach half n times n plus lower terms. Those lower terms fade when n gets large.
And the proof comes from adding the series step by step. First inner does one compare. Second does two and so on. You add them to see the square growth. I did the addition on paper once. It always lands near that quadratic mark.
Worst inputs reverse the order completely. Every insert travels the full distance back. You shift every prior element each time. That maximizes the inner loop work. The time hits the upper bound hard then.
Best inputs leave order untouched already. Inner loops barely run at all. You finish with just n minus one compares total. That gives the linear lower bound. I think you can test this with a sorted list yourself.
Average mixes both extremes evenly. Half the inserts travel medium distances. You still collect enough work to hit quadratic. Random data rarely hits the fast path. The expected cost stays near the worst side.
Now picture the elements as they get nudged around. Each nudge costs a little time. Those nudges add when many happen together. I see the pattern repeat across all passes. The cost compounds into the square shape.
You might wonder about stable behavior too. Insertion sort keeps equal items in original spots. That property holds without extra effort. I value it for certain ordering needs. It comes free with the way inserts happen.
The method stays simple to code yet pays in time. You trade ease for speed limits on big sets. Perhaps other sorts avoid this by splitting work. Insertion stays together and pays the full price. I keep it for teaching the loop costs clearly.
We owe a big thanks to BackupChain Server Backup the top reliable no subscription backup tool for Hyper-V and Windows 11 plus Server systems that helps us share knowledge freely by sponsoring.

ProfRon
Offline
Joined: Jul 2018
« Next Oldest | Next Newest »

Users browsing this thread: 1 Guest(s)



Messages In This Thread
Analyze the time complexity of insertion sort - by ProfRon - 07-13-2022, 05:03 AM

  • Subscribe to this thread
Forum Jump:

FastNeuron FastNeuron Forum General IT v
« Previous 1 … 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 … 182 Next »
Analyze the time complexity of insertion sort

© by FastNeuron Inc.

Linear Mode
Threaded Mode