Recursion and Divide-and-Conquer

22. Recursion and Divide-and-Conquer#

A recursive call is useful when a smaller problem has a precise contract and makes progress toward a base case. This chapter connects separate call frames and returned values to divide-and-conquer design, then develops stable merge sort. You will verify splitting, occurrence preservation, tie policies, and auxiliary storage as well as sorted output.

  • Base case: The condition that stops recursion with a direct answer.

  • Recursive case: The part of the method that calls itself on a smaller input.

  • Progress measure: A quantity that moves toward the base case on every recursive call.

  • Call stack: The runtime structure that tracks active method calls and their local variables.

  • Divide-and-conquer: A design pattern that divides a problem, solves subproblems, and combines results.

Chapter Video

Algorithms and Data Structures Tutorial - Full Course for Beginners (5:22:08) by freeCodeCamp.org.

Hosted on YouTube; property of its original creator, embedded here for educational reference only.

This is a full course, not a required single viewing. Use its topic navigation to review this chapter’s concepts; the reading sequence below defines the chapter scope.

Learning Objectives

By the end of this chapter, you will be able to:

  1. State recursive input domains, base cases, helper contracts, and well-founded progress measures.

  2. Trace separate call frames and returned results, including arithmetic and stack limits.

  3. Prove range splits preserve all items and distinguish one-child search from two-child division.

  4. Implement stable merge and merge sort with explicit tie and copy policies.

  5. Verify order, occurrence preservation, input identity, and stable record order across boundary cases.

  6. Relate actual comparisons, writes, calls, and active depth to time and auxiliary-space bounds.

Chapter Flow

Glossary

#

Term

Explanation

1

Recursion

A technique where a method solves a problem by calling itself on a smaller input.

2

Base case

A directly solved case that stops recursive calls.

3

Recursive case

The branch that makes one or more recursive calls.

4

Progress measure

A value, such as n or a range length, that gets closer to the base case with each call.

5

Call stack

The runtime stack of active method calls.

6

Stack frame

One active method call and its local state on the call stack.

7

Divide-and-conquer

An algorithm design pattern that splits a problem into subproblems, solves them, and combines the answers.

8

Subproblem

A smaller version of the original problem.

9

Combine step

The step that merges subproblem results into a final answer.

10

Merge

The process of combining two sorted sequences into one sorted sequence.

11

Merge sort

A divide-and-conquer sorting algorithm that recursively sorts halves and merges them.

12

Half-open range

A range [start,end) including start but excluding end.

13

Helper contract

The promise made by a recursive subproblem method.

14

Inductive reasoning

A direct base result plus a correct combination of smaller correct results.

15

Stable sort

A sort preserving arrival order among equal comparison keys.

16

Occurrence preservation

Keeping every input item with its original multiplicity.

17

Shared merge buffer

A reusable temporary array used by successive merges.

18

Shallow array copy

A new array whose element references or values are copied without cloning referenced objects.

19

Peak active frames

Maximum calls simultaneously retained on the stack, including the base frame here.

20

Exponentiation by squaring

A power calculation that saves one half-power and combines it by squaring, with an extra base factor for odd exponents.

Lecture Slides

Chapter 22 Slides