Skip to content

Divide and Conquer (1)

Course: Recursion - chapter 3

Divide and Conquer is a group of algorithms that rely strongly on recursions. It follows a 2 step idea:

  1. Divide a problem into smaller subproblems
  2. Combine the solutions of the subproblems to solve the bigger problem

Let’s understand this with the merge sort algorithm 🙂

Q - You are given an array A of size N. Sort the array

Let’s understand how D&C fits in. We need to sort the entire array of N elements. What we can do is, break the array into 2 smaller arrays and if we can sort these 2 smaller arrays, we can try to find a way to combine them to form a single sorted array.

Let’s assume we have a combine method that takes in 2 sorted arrays and combines them into a single sorted array.

combine(arr1, arr2) -> returns arr which is a sorted array of combined arr1 and arr2

Now essentially, what we do is

1. sort(arr) where arr is of length n
2. Break arr into 2 subarrays arr2 and arr3 of size n/2 each.
3. sort(arr2) and sort(arr3)
4. combine(arr2, arr3) -> Outputs sorted arr

So, we have broken down the problem (i.e. sorting an array of length n) into 2 subproblems (sorting an array of length n/2) and then merged these sorted arrays into a single sorted array.

Base case:

When we are left with a single element, there is nothing to divide or sort. So just return in that case.

void sort(arr, beg, end) {
// ---------- BASE CASE ------------
if (beg == end) {
// only single element. No need to sort it or combine it
return;
}
// Sorts array from index beg to end
// -------- DIVIDE STAGE ---------------
int mid = (beg + end) / 2;
// ------------ CONQUER STAGE -----------------
sort(arr, beg, mid); // Sort the first half
sort(arr, mid+1, end); // Sort the second half
// ------------ MERGE STAGE -----------------
combine(arr, beg, mid, end); // Combine the 2 sorted arrays into single sorted array
}
sort(arr, 0, n-1); // Sort the entire array

Read how we can combine efficiently here: https://www.geeksforgeeks.org/merge-two-sorted-arrays/

Q - Let’s solve this problem using Divide and Conquer: https://leetcode.com/problems/number-of-1-bits/description/

Divide and Conquer is a group of algorithms that rely strongly on recursions. It follows a 2 step idea:

  1. Divide a problem into smaller subproblems
  2. Combine the solutions of the subproblems to solve the bigger problem

Let’s understand this with the merge sort algorithm 🙂

Q - You are given an array A of size N. Sort the array

Let’s understand how D&C fits in. We need to sort the entire array of N elements. What we can do is, break the array into 2 smaller arrays and if we can sort these 2 smaller arrays, we can try to find a way to combine them to form a single sorted array.

Let’s assume we have a combine method that takes in 2 sorted arrays and combines them into a single sorted array.

combine(arr1, arr2) -> returns arr which is a sorted array of combined arr1 and arr2

Now essentially, what we do is

1. sort(arr) where arr is of length n
2. Break arr into 2 subarrays arr2 and arr3 of size n/2 each.
3. sort(arr2) and sort(arr3)
4. combine(arr2, arr3) -> Outputs sorted arr

So, we have broken down the problem (i.e. sorting an array of length n) into 2 subproblems (sorting an array of length n/2) and then merged these sorted arrays into a single sorted array.

Base case:

When we are left with a single element, there is nothing to divide or sort. So just return in that case.

void sort(arr, beg, end) {
// ---------- BASE CASE ------------
if (beg == end) {
// only single element. No need to sort it or combine it
return;
}
// Sorts array from index beg to end
// -------- DIVIDE STAGE ---------------
int mid = (beg + end) / 2;
// ------------ CONQUER STAGE -----------------
sort(arr, beg, mid); // Sort the first half
sort(arr, mid+1, end); // Sort the second half
// ------------ MERGE STAGE -----------------
combine(arr, beg, mid, end); // Combine the 2 sorted arrays into single sorted array
}
sort(arr, 0, n-1); // Sort the entire array

Read how we can combine efficiently here: https://www.geeksforgeeks.org/merge-two-sorted-arrays/

Q - Let’s solve this problem using Divide and Conquer: https://leetcode.com/problems/number-of-1-bits/description/