ExamplesBy LevelBy TopicLearning Paths
840 Fundamental GitHub

Divide and Conquer Pattern

Functional Programming

Tutorial Video

Text description (accessibility)

This video demonstrates the "Divide and Conquer Pattern" functional Rust example. Difficulty level: Fundamental. Key concepts covered: Functional Programming. Divide and conquer is an algorithm design paradigm that decomposes a problem into smaller independent subproblems, solves them recursively, and combines solutions. Key difference from OCaml: | Aspect | Rust | OCaml |

Tutorial

The Problem

Divide and conquer is an algorithm design paradigm that decomposes a problem into smaller independent subproblems, solves them recursively, and combines solutions. It underlies merge sort, quicksort, binary search, FFT, Strassen matrix multiplication, and closest pair of points. The recurrence T(n) = aT(n/b) + f(n) is analyzed by the Master Theorem. Understanding divide and conquer as a pattern — not just specific algorithms — enables recognizing and applying it to new problems. Many problems that appear to require O(n^2) naive solutions admit O(n log n) or better divide-and-conquer solutions.

🎯 Learning Outcomes

  • • Identify the three components: divide (split problem), conquer (recursive solve), combine (merge solutions)
  • • Apply Master Theorem to analyze recurrences: T(n) = aT(n/b) + O(n^k)
  • • Implement generic divide-and-conquer scaffolding with configurable split, solve, and merge operations
  • • Recognize when divide-and-conquer helps: independent subproblems with efficient merge
  • • Compare with dynamic programming: D&C has independent subproblems; DP has overlapping ones
  • Code Example

    #![allow(clippy::all)]
    //! Placeholder

    Key Differences

    AspectRustOCaml
    Generic params<T, R, F, G, H> with trait boundsPolymorphic 'a -> 'b
    Function passingReferences to closuresFirst-class functions
    Array splittingSlice references &[T]Array.sub copies
    Parallelismrayon::joinDomain.spawn (OCaml 5.0)
    Tail recursionNot applicable (tree recursion)Not TCO here
    Pattern recognitionMaster TheoremSame analysis

    OCaml Approach

    OCaml's higher-order functions make the D&C pattern elegant: let rec dc base_case divide combine items = if small items then base_case items else let (l, r) = divide items in combine (dc base_case divide combine l) (dc base_case divide combine r). The divide_and_conquer function is naturally polymorphic. OCaml's Array.sub for splitting and @ or Array.append for combining arrays. The parallel version uses Domain.spawn (OCaml 5.0) to run both halves concurrently.

    Full Source

    #![allow(clippy::all)]
    //! Placeholder

    Deep Comparison

    OCaml vs Rust: Divide And Conquer Pattern

    Overview

    See the example.rs and example.ml files for detailed implementations.

    Key Differences

    AspectOCamlRust
    Type systemHindley-MilnerOwnership + traits
    MemoryGCZero-cost abstractions
    MutabilityExplicit refmut keyword
    Error handlingOption/ResultResult<T, E>

    See README.md for detailed comparison.

    Exercises

  • Implement merge sort using the divide_and_conquer generic function with appropriate divide and combine closures.
  • Implement binary search as a divide-and-conquer: divide splits at midpoint, combine picks the recursive result.
  • Apply divide and conquer to count inversions in an array in O(n log n) by modifying merge sort.
  • Parallelize the divide step using Rayon's join and measure speedup on merge sort for n = 10^7.
  • Implement the divide-and-conquer closest pair using the generic scaffolding above with appropriate functions.
  • Open Source Repos