Case Analysis: Common Algorithms
Whirl a stone tied to a string above your head and you can feel the string biting into your fingers. Cut the string and the stone flies off along a tangent — not outward, but sideways. That tiny experiment hides everything you need to know about circular motion.
Definition: Uniform circular motion is the motion of a particle along a circular path at constant speed.
Definition: Centripetal acceleration is the acceleration of a particle in circular motion that always points from the particle toward the centre of the circle.
Definition: Centripetal force is the net real force that produces centripetal acceleration; it is the cause, not a separate force category.
Why circular motion needs acceleration at all
In Newtonian mechanics, acceleration means a change in the velocity vector. Velocity has two ingredients: magnitude (speed) and direction. In uniform circular motion the speed never changes, but the direction of the velocity changes every instant. The velocity vector at the top of a circle points one way; a moment later it points slightly sideways. Any change in a vector is a non-zero acceleration, so even at constant speed there must be an acceleration. By Newton's second law, that acceleration must be produced by a net force.
A careful geometric argument (drawing two velocity vectors at nearby instants and looking at the small triangle they form with Δv) gives the magnitude of this acceleration as:
a_c = v² / r = ω² r
where v is the linear speed, r is the radius of the circle and ω = v / r is the angular speed in radians per second. The direction is always toward the centre — hence the name centripetal, from the Latin centrum (centre) + petere (to seek).
The corresponding centripetal force required is:
F_c = m a_c = m v² / r = m ω² r
Centripetal force is a role, not an identity
This is the single idea most NEET aspirants get wrong. There is no special "centripetal force" sitting in the universe alongside gravity and tension. Centripetal force is the job description of whichever real force happens to point toward the centre. In different problems the role is played by different real forces:
- A satellite orbiting Earth → gravity is the centripetal force.
- A car turning on a flat road → friction between tyre and road is the centripetal force.
- A stone whirling on a string → tension in the string is the centripetal force.
- A bead inside a smooth bowl tracing a horizontal circle → the horizontal component of the normal force is the centripetal force.
- An electron held in orbit (Bohr-style) → the Coulomb attraction is the centripetal force.
So in any problem you should first identify which real forces act on the body, then check which one (or which component) points toward the centre, and then equate that to m v² / r.
The centrifugal "force" — a useful illusion
When you sit inside a turning auto-rickshaw and feel pushed outward, you are not really being pushed by anything — you are simply trying to continue in a straight line while the floor of the auto curves under you. To make Newton's laws look correct from inside the rotating auto (a non-inertial frame), physicists invent a fictitious outward force called the centrifugal force. It is a pseudo-force; it does not exist in an inertial (ground) frame.
For NEET and JEE, the rule is strict: work in the ground frame and never write a centrifugal force in your free-body diagram. Only the inward-pointing real force appears, and you set it equal to m v² / r.
Linking speed, radius and time
Two relations tie circular motion together neatly:
v = r ω (linear speed = radius × angular speed)
T = 2π / ω = 2π r / v (time period of one revolution)
Frequency f = 1 / T, and ω = 2π f. These let you slide between "how fast in m/s" and "how many revolutions per second" without re-deriving anything.
Why it matters: Circular motion is the bridge between Class XI kinematics and the rest of mechanics — planetary motion, banking of roads, vertical loops, conical pendulums, charged particles in magnetic fields and even Bohr's atom all reduce to F_c = m v² / r once you spot the circle.
Real-world example: When a Mumbai local train rounds a curve, the outer rail is laid slightly higher than the inner rail — this is called superelevation or cant. The horizontal component of the normal force from the canted track supplies the centripetal force, so the train does not depend solely on flange friction. Indian Railways designs the cant for a specific design speed using exactly v² / r.
Common misconception: "There is an outward centrifugal force throwing the car off the road." Wrong. In the ground frame there is only an inward friction force; when that friction is insufficient (wet road, high speed) the car simply continues tangentially in a straight line because no centripetal force is left to bend its path. The car slides outward not because something pushes it out but because nothing pulls it in any longer.
Question: A 1200 kg car takes a flat circular turn of radius 50 m at 15 m/s. What centripetal force is required, and what is the minimum coefficient of friction needed?
Solution:
Step 1: Required centripetal force F_c = m v² / r = 1200 × (15)² / 50 = 1200 × 225 / 50 = 5400 N.
Step 2: On a flat road, friction supplies this force. Maximum friction = μ m g = μ × 1200 × 10 = 12000 μ N.
Step 3: For the car to just hold the turn, 12000 μ ≥ 5400, so μ ≥ 0.45.
Conclusion: A coefficient of friction of at least 0.45 is needed; a wet road with μ ≈ 0.3 would cause the car to skid outward.
| Feature | Centripetal force | Centrifugal force |
|---|---|---|
| Nature | Real, identifiable | Pseudo / fictitious |
| Direction | Toward centre | Away from centre |
| Frame of reference | Inertial (ground) | Non-inertial (rotating) |
| Required for Newton's laws? | Yes | Only inside rotating frame |
| Example | Tension, gravity, friction | Feeling thrown outward in a turn |
- ✓- Uniform circular motion has constant speed but continuously changing velocity, so it is accelerated motion.
- ✓- Centripetal acceleration a_c = v² / r = ω² r and it always points toward the centre.
- ✓- Required centripetal force F_c = m v² / r is supplied by a real force — tension, gravity, friction or normal.
- ✓- v = r ω and T = 2π r / v link linear and angular quantities.
- ✓- Centrifugal force is a pseudo-force; do not include it in inertial-frame free-body diagrams.
- ✓- Increasing speed quadruples the force needed for the same radius (F ∝ v²).
- ✓- Halving the radius doubles the force needed at the same speed.
- ✓- A body in circular motion is never in equilibrium — there is always a non-zero net inward force.
"Centripetal points to Centre, Centrifugal is Counterfeit." Both words start with centri-, but -petal (Latin petere, to seek) reaches inward to the centre, while -fugal (Latin fugere, to flee) only seems to flee outward — and only inside a rotating elevator of the mind.
- ✓- Circular motion always needs a net inward (centripetal) force.
- ✓- That force is whatever real force happens to point to the centre — no new force is invented.
- ✓- Magnitude: F_c = m v² / r = m ω² r; direction: toward the centre.
- ✓- Centrifugal force is a pseudo-force, valid only in a rotating frame, never in NEET inertial-frame solutions.
Big-O vs Worst Case: A Crucial Distinction
Big-O is NOT the same as worst case. Big-O/Omega/Theta are bounds on a FUNCTION; best/average/worst describe which INPUT you analyze. You can state Big-O of the best case (e.g., insertion sort best case is Theta(n), which is also O(n) and O(n^2)).
Thus 'worst case is O(n^2)' and 'best case is Omega(n)' are both valid. A bound and a case are orthogonal concepts.
Common GATE trap: 'The worst-case running time of merge sort is O(n^2)' is TECHNICALLY TRUE (since O is an upper bound, and n log n <= c.n^2), even though Theta is n log n. Read whether the question asks for tight (Theta) or just an upper bound (O).
Sorting/Searching Complexity Cheat Sheet
In GATE CSE, sorting and searching questions almost never ask you to invent an algorithm. They ask whether you remember the exact running time, the exact space, and the exact number of comparisons. Treat this lesson as a cheat sheet you can recall in seconds during the exam, and as the proof-sketches that justify why each number is what it is.
Definition: A comparison sort is any algorithm whose only operation on input elements is "compare two of them and branch." Merge sort, quicksort, heap sort, insertion sort, bubble sort and selection sort are all comparison sorts.
Definition: A non-comparison sort uses the value of the keys (their bits or digits or buckets), not just the result of comparisons. Counting, radix and bucket sorts are the standard examples.
The Omega(n log n) lower bound for comparison sorts
Any comparison-based sorting algorithm can be drawn as a decision tree: each internal node asks "is a[i] < a[j]?" and each leaf is one possible permutation of the input. There are n! permutations, so the tree needs at least n! leaves. A binary tree with L leaves has height at least ceil(log2 L), so the height — which equals the worst-case number of comparisons — is at least log2(n!).
By Stirling's approximation, log2(n!) = Theta(n log n). So no comparison sort can do better than Omega(n log n) in the worst case. This is why heap sort and merge sort are called "optimal comparison sorts": they hit this bound.
Why it matters: This single sentence answers a huge family of GATE MCQs of the form "what is the minimum number of comparisons needed to sort n elements in the worst case?" The answer is always ceil(log2(n!)), which for n = 5 is 7, for n = 12 is 29, and so on. If an option promises o(n log n) for a comparison sort, it is wrong by this theorem.
Beating Omega(n log n): the non-comparison sorts
Non-comparison sorts escape the decision-tree argument because they do not make comparisons in the first place; they look at the keys directly.
- Counting sort: O(n + k), where k is the range of input values. Stable. Needs O(n + k) space. Useless when k is huge (e.g. 32-bit integers) but ideal when k = O(n).
- Radix sort: O(d (n + k)), where d is the number of digits and k is the base. Uses a stable sort (usually counting sort) per digit. For fixed-width integers d is a constant, so radix is effectively O(n).
- Bucket sort: average O(n) when inputs are drawn from a uniform distribution and bucketed into n buckets; worst case degrades to O(n^2) if everything piles into one bucket.
Common misconception: Students sometimes write "radix sort is O(n) always, so it beats merge sort." Strictly, radix is O(d(n+k)) and assumes the keys fit in a bounded number of digits. For arbitrary real numbers or arbitrary-length strings, radix is not magically O(n).
Space complexity you must remember
| Algorithm | Time (avg) | Time (worst) | Aux space | Stable | In-place |
|---|---|---|---|---|---|
| Merge sort | n log n | n log n | O(n) | Yes | No |
| Quicksort | n log n | n^2 | O(log n)* | No | Yes |
| Heap sort | n log n | n log n | O(1) | No | Yes |
| Insertion sort | n^2 | n^2 | O(1) | Yes | Yes |
| Selection sort | n^2 | n^2 | O(1) | No | Yes |
| Bubble sort | n^2 | n^2 | O(1) | Yes | Yes |
| Counting sort | n + k | n + k | O(n + k) | Yes | No |
| Radix sort | d(n + k) | d(n + k) | O(n + k) | Yes | No |
*The O(log n) for quicksort is the recursion stack depth on average. Worst-case stack depth is O(n) (e.g. already-sorted input with naive pivot).
Selection sort: the algorithm GATE loves to ask about
Selection sort is Theta(n^2) in all three cases — best, average and worst. This is unusual; insertion sort, by contrast, is O(n) on already-sorted input. The reason: selection sort always scans the unsorted suffix to find the minimum, no matter what the data looks like.
Its redeeming feature: it makes only n - 1 swaps in total, because after finding the minimum of the suffix it does exactly one swap. So when write cost dominates read cost (e.g. flash memory or EEPROM), selection sort can actually be the right pick. This is a favourite "best use case" trick question.
Key numerical results you must memorise
These exact formulas show up almost every year in some form.
- Minimum comparisons to find both min and max in n elements:
ceil(3n/2) - 2. Intuition: pair the elements up, compare within each pair (n/2 comparisons), then run min-tournament on the smaller-of-each-pair and max-tournament on the larger-of-each-pair (n/2 - 1 comparisons each). Total: 3n/2 - 2 for even n. - Minimum comparisons to find the 2nd smallest element:
n + ceil(log2 n) - 2. Intuition: run a knock-out tournament (n - 1 comparisons) to find the smallest. The 2nd smallest must have lost a comparison to the smallest at some point in the tournament; there are at mostceil(log2 n)such candidates, and finding the minimum of them costsceil(log2 n) - 1more comparisons. - Merging two sorted lists of sizes m and n:
m + n - 1comparisons worst case. Intuition: in the worst case every comparison consumes only one element from one list; the last element does not need a comparison.
Worked example:
Question: A tournament-style algorithm is used to find the smallest of 16 numbers, and then the second-smallest. What is the total number of comparisons needed?
Solution:
Step 1: Smallest by knock-out tournament: 16 - 1 = 15 comparisons.
Step 2: Number of candidates for 2nd smallest = log2(16) = 4 (these are the elements that lost directly to the eventual smallest).
Step 3: Finding the minimum of those 4 candidates: 4 - 1 = 3 comparisons.
Conclusion: Total = 15 + 3 = 18, which matches n + ceil(log2 n) - 2 = 16 + 4 - 2 = 18.
Real-world example: When the Linux kernel sorts small integer arrays in lib/sort.c, it falls back to insertion sort for small inputs and uses heap sort for large ones — exactly because heap sort is the only one with O(n log n) worst-case time and O(1) auxiliary space, both essential inside a kernel where stack space is tight.
Searching: the smaller sibling
- Linear search: O(n) on any array.
- Binary search: O(log n) on a sorted array. Requires random access (works on arrays, not on linked lists in O(log n)).
- Ternary search: O(log3 n) — same Big-O class as binary search, but more comparisons per level; usually slower in practice.
Common misconception: "Binary search is O(log n) on a linked list too." It is not; following links is O(n). On a sorted linked list you cannot do better than linear search.
| Property | Merge sort | Quicksort | Heap sort |
|---|---|---|---|
| Worst-case time | n log n | n^2 | n log n |
| Auxiliary space | O(n) | O(log n) | O(1) |
| Stable | Yes | No | No |
| In-place | No | Yes | Yes |
| Cache-friendly | Moderate | Excellent | Poor |
- ✓- Comparison sorts cannot beat Omega(n log n); proof is by decision-tree height = log2(n!).
- ✓- Counting, radix and bucket escape the bound by using key values, not comparisons.
- ✓- Heap sort = the only in-place O(n log n) worst-case comparison sort.
- ✓- Quicksort is fastest in practice but O(n^2) worst case; randomised pivot makes worst case extremely unlikely.
- ✓- Selection sort is O(n^2) in all cases but uses only O(n) swaps.
- ✓- Min + Max in
ceil(3n/2) - 2comparisons; 2nd smallest inn + ceil(log2 n) - 2. - ✓- Merging m and n sorted items: m + n - 1 comparisons worst case.
- ✓- Stability matters when sorting by a secondary key after the primary key.
"MR HC SI B" — Memorise the in-place column: Merge no, Radix no; Heap yes, Counting no, Selection yes, Insertion yes, Bubble yes.
For the "min + max" formula, remember the picture: pair up (n/2 comparisons), race the small ones for min, race the big ones for max (n/2 - 1 each). Total cost = n/2 + 2(n/2 - 1) = 3n/2 - 2.
- ✓- Comparison-sort lower bound: Omega(n log n), proved by decision trees.
- ✓- Non-comparison sorts (counting, radix, bucket) can be linear under the right conditions.
- ✓- Heap sort is the textbook answer when you need O(n log n) worst case and O(1) space.
- ✓- Memorise the comparison-count formulas — they are pure recall marks in GATE.
Best, Average, Worst Case & Growth — Flashcards
Cover the answer, recall, then check. 12 GATE cards on case analysis and growth comparison.
Q1. Quicksort: best, average, worst time?
A1. Best Θ(n log n), average Θ(n log n), worst Θ(n²) (already-sorted input with naïve pivot).
Q2. Insertion sort: best, average, worst?
A2. Best Θ(n) (already sorted), average Θ(n²), worst Θ(n²) (reverse sorted).
Q3. Merge sort: best, average, worst?
A3. All three Θ(n log n) — input-independent.
Q4. Heapsort: best, average, worst?
A4. All three Θ(n log n).
Q5. Selection sort: best, average, worst?
A5. All three Θ(n²) — it always scans the full unsorted part.
Q6. Linear search: best, average, worst?
A6. Best Θ(1), average Θ(n), worst Θ(n).
Q7. Binary search: worst-case time and space?
A7. Θ(log n) time; Θ(1) iterative / Θ(log n) recursive stack space.
Q8. Bubble sort with an early-exit flag: best case?
A8. Θ(n) — one clean pass on sorted input; worst still Θ(n²).
Q9. Difference between average-case and randomized (expected) analysis.
A9. Average-case assumes a distribution over inputs; randomized/expected averages over the algorithm's random choices, holding the input fixed (e.g. randomized quicksort is Θ(n log n) expected on any input).
Q10. Compare growth: n log n vs n^1.5.
A10. n log n grows slower (log n = o(√n)), so n log n = o(n^1.5).
Q11. Compare 2ⁿ vs n!.
A11. n! grows faster for n ≥ 4; n! = ω(2ⁿ) (ratio n!/2ⁿ → ∞).
Q12. Compare n^(log n) vs (log n)ⁿ.
A12. (log n)ⁿ grows faster: logs give (log n)² vs n·log log n, and n log log n dominates.
Best, Average, Worst Case and Growth Comparison — Worked Example
Worked Example
Problem: For a linear (sequential) search of a target value in an unsorted array of n distinct elements, determine the number of comparisons in the best case, the worst case, and the average case (assuming the target is present and equally likely to be at any position).
Solution:
Linear search scans elements one by one until it finds the target.
Best case: the target is the very first element checked.
Comparisons = 1 ⇒ O(1).
Worst case: the target is the last element (or absent), so all elements are examined.
Comparisons = n ⇒ O(n).
Average case: if the target is equally likely to be in any of the n positions, the expected number of comparisons is the average of 1, 2, …, n:
Average = (1 + 2 + … + n)/n = [n(n + 1)/2]/n = (n + 1)/2 ⇒ Θ(n).
Answer: Best = 1 comparison, Worst = n comparisons, Average = (n + 1)/2 comparisons (both worst and average are Θ(n)).
- ✓- Best case is the most optimistic input, worst case the most pessimistic; average case weights inputs by their probability.
- ✓- Algorithm guarantees quote the worst case (an upper bound valid for every input); average case needs an input distribution.
- ✓- Linear search is O(n); the same case analysis distinguishes, e.g., quicksort's O(n log n) average from its O(n²) worst case.