101 Logo
onenoughtone

Interactive Divide and Conquer Visualization

Merge Sort

Watch how merge sort divides an array into smaller subarrays, sorts them, and then merges them back together.

Interactive Visualization:

Merge Sort Visualization

Merge Sort: A divide-and-conquer algorithm that divides the input array into two halves, recursively sorts them, and then merges the sorted halves.

48
0
4
1
75
2
83
3
38
4
70
5
95
6
72
7
Unsorted
Sorted
Highlighted
Pivot
Comparing
Swapping
Merging

Time Complexity

  • Best Case: O(n log n)
  • Average Case: O(n log n)
  • Worst Case: O(n log n)

Space Complexity

  • Auxiliary Space: O(n)
  • Requires additional space for the temporary arrays during merging

Step 0 of 84: Initial state

Quick Sort

See how quick sort partitions an array around a pivot element and recursively sorts the resulting subarrays.

Interactive Visualization:

Quick Sort Visualization

Quick Sort: A divide-and-conquer algorithm that picks an element as a pivot and partitions the array around the pivot, placing smaller elements before it and larger elements after it.

30
0
31
1
80
2
83
3
68
4
90
5
62
6
68
7
Unsorted
Sorted
Highlighted
Pivot
Comparing
Swapping
Merging

Time Complexity

  • Best Case: O(n log n)
  • Average Case: O(n log n)
  • Worst Case: O(n²) - when the array is already sorted and using first/last element as pivot

Space Complexity

  • Auxiliary Space: O(log n) - for the recursion stack
  • In-place sorting algorithm (doesn't require additional arrays)

Pivot Selection Strategies

  • First Element: Simple but can lead to O(n²) time for sorted arrays
  • Last Element: Simple but also can lead to O(n²) time for sorted arrays
  • Middle Element: Better performance for sorted or nearly sorted arrays
  • Random Element: Good average performance, avoids worst-case scenarios
  • Median of Three: Chooses the median of first, middle, and last elements, reducing the chance of picking a bad pivot

Step 0 of 51: Initial state

Closest Pair of Points

Explore how the closest pair of points algorithm divides the plane into two halves and finds the closest pair efficiently.

Interactive Visualization:

Closest Pair of Points Visualization

Closest Pair of Points: A divide-and-conquer algorithm that finds the pair of points with the smallest distance between them in a set of points in a plane.

Normal Point
Left Half
Right Half
In Strip
Comparing
Closest Pair

Time Complexity

  • Brute Force: O(n²)
  • Divide and Conquer: O(n log n)
  • Sorting points takes O(n log n)
  • Recursive calls take T(n) = 2T(n/2) + O(n)

Algorithm Steps

  1. Sort points by x-coordinate
  2. Divide the points into two halves
  3. Recursively find the closest pair in each half
  4. Find the minimum distance d from the two halves
  5. Create a strip of points within d distance of the dividing line
  6. Find the closest pair in the strip (if any)
  7. Return the overall closest pair

Key Insight

The key insight of this algorithm is that we only need to check a limited number of points in the strip. It can be proven mathematically that for each point in the strip, we only need to check at most 7 points ahead of it (in y-coordinate order). This is because if we have more points within a d×d square, at least two of them must be closer than distance d.

Step 0 of 54: Initial state

IntroVisualizePatternsPractice
101 Logo
onenoughtone