Divide and Conquer

  • Divide

    • Divide the problem to solve into several small parts.

  • Conquer

    • Solve each small divided problem

  • Combine

    • (If necessary) collect the solved answers

Top-down Approach

: Divide and conquer is a top-down approach

Exponentiation

  • Iterative algorithm: O(n)

    • C to the power = multiply C by the value to be raised to the power starting from 1

  • Divide and conquer based algorithm: O(logn)

    • Solve by dividing the exponent in half and multiplying

    • When the exponent becomes 1, return the base directly

      • Time complexity can be reduced to O(log n)!

Merge Sort

  • A method of merging multiple sorted data sets into one sorted set

  • Utilizing divide and conquer algorithms

    • Divide data into minimum unit problems, then sort in order to get final results

    • top-down approach

  • Time complexity

    • O(nlogn)

Example)

Quick Sort

  • Divide the given array into two and sort each

  • Differences from merge sort

    1. While merge sort just divides into two parts, quick sort positions items smaller than the pivot item on the left and larger items on the right when dividing

    2. After each part is sorted, merge sort requires "merging" post-processing work, but quick sort does not require it.

  • Time complexity

    • nlogn

Hoare-Partition Algorithm

Idea

  • Position values larger than P(pivot) in the right set and smaller values in the left set

  • Position the pivot in the middle of the two sets

Pivot Selection

  • Left end / Right end / Middle value among three arbitrary values

Example)

  • A method of determining the next search position by comparing with the key value of the item in the middle of the data and continuing the search

    • By recursively repeating binary search until the target key is found, the search range is halved to perform faster searches

  • For binary search, the data must be in a sorted state.

Search Process

  1. Select the element in the center of the data

  2. Compare the value of the central element with the target value to find

  3. If the target value is compared to the central element value

    • If smaller, perform new search on the left half of the data

    • If larger, perform new search on the right half of the data

Last updated