03. Divide and Conquer
03. Divide and Conquer
Divide and Conquer is an algorithmic approach that primarily employs recursion. Some can be solved
using iteration.
● Second, identify the base case. A base case is the given problem in a smaller size such that it can
be solved without any computation.
○ For example, for the count-even-numbers problem, if you have to count the even
numbers of an array of size 1 (e.g. Arr={3}), then it's easy. if that one element is an even
then you return 1, else 0. Therefore, now the code looks like following
Page 1
Divide and conquer |Algo Lab | Fariha Tabassum Islam
if ( Arr[i] % 2 == 0 ) return 1;
else return 0;
}
}
● Next, for solving the problem when the base case scenario does not occur, suppose if you
already know the solution of a problem of size N/2. Now, can you find the solution of a bigger
problem of size N?
○ For example, suppose, you already know the count of even numbers in the first half of
the array which is 0, and you also know the count of the even numbers in the second
half of the array which is 2. Then, can you tell me the count of even numbers in the
whole array?
■ Yes, you can. It is 0+2=2.
○ So, how can we actually know the count of even numbers in the first half and the
second half? (Divide and Conquer)
■ Using recursion
Page 2
Divide and conquer |Algo Lab | Fariha Tabassum Islam
Practice problems:
PROBLEM 01.
Write a function print_odd using divide-and-conquer algorithm to print the odd numbers of an array
of n integers.
PROBLEM 02.
Write a function calc_sum using divide-and-conquer algorithm to calculate the sum of an array of n
integers.
PROBLEM 03.
Write a function calc_sum using divide-and-conquer algorithm to calculate the sum of the even
numbers of an array of n integers.
3 7 2187
Hint:
Page 3
Divide and conquer |Algo Lab | Fariha Tabassum Islam
(ii) write a function findMaxMin that returns the maximum and minimum elements of an array using
divide and conquer.
(iii) use the function findMaxMin to print the maximum and minimum elements of the array A
6 max: 34.0
34 -1.5 5 6 -50.1 -6 min: -50.1
Pseudocode:
Page 4
Divide and conquer |Algo Lab | Fariha Tabassum Islam
Write a main that takes the array A and an integer X as input from the user. After that, sort the array A
using the quicksort algorithm and find the index of X in A using the function binary_search and print it.
5 4 found in index 2
3 4 5 7 2
4
5 14 not found
3 4 5 7 2
14
Pseudocode:
Note that the following pseudocode assumes that the array is sorted in ascending order.
Function BinarySearch( A, i, j, X) {
1. if i==j then
2. if X == A[i] then return i
3. else return NOT_FOUND
4. else
5. mid = (i+j)/2
6. if X == A[mid] then return mid
7. else if X < A[mid] then
8. return BinarySearch(A, i, mid-1, X)
9. else // if X > A[mid]
10. return BinarySearch(A, mid+1, j, X)
11. end if
}
More concise version:
Page 5
Divide and conquer |Algo Lab | Fariha Tabassum Islam
4 7 5 3 -1
3 7 5 -1
Pseudocode (ascending order): Note that following pseudocode assumes that indexing starts
from 1
Page 6
Divide and conquer |Algo Lab | Fariha Tabassum Islam
Write a function count_inversion that counts the inversions in an array of N numbers using divide and
conquer. Write a main function that takes N numbers from users and uses the function count_inversion
to count the number of inversions and print it.
5 #inversions: 6
8 4 -1 2 5
7 #inversions: 10
1 20 6 4 5 8 4
10 #inversions: 23
1 20 6 4 5 8 4 6 2 5
Hint: The solution is similar to merge-sort. Merge two sorted lists into one output list, but while doing
so, we also count the inversion (in line 16 of the merge function in the pseudocode).
Page 7
Divide and conquer |Algo Lab | Fariha Tabassum Islam
1. Select an element q, called a pivot, from the array. In this algorithm we have chosen the last
index as the pivot.
2. The PARTITION function finds the location of the pivot in such a way that all the elements
smaller than the pivot are on the left side and all the elements on the right-hand side of the
pivot are greater in value. (Items with equal values can go either way).
3. Recursively call the QUICKSORT function which performs quicksort on the array on the left
side of the pivot and then on the array on the right side, thus dividing the task into sub tasks.
This is carried out until the arrays can no longer be split.
Write a function quick_sort that sorts an array of N numbers in descending order using quicksort. Write
a main that takes N numbers as input from users into an array, sorts the array in descending order using
the function quick_sort, and prints the sorted array.
4 7 5 3 -1
3 7 5 -1
9 341 45 45 31 5 3 -1 -13
45 341 -1 45 3 31 -13 -134 5
Page 8
Divide and conquer |Algo Lab | Fariha Tabassum Islam
Write a function find_max_sum_subarray that finds the maximum sum subarray of an array A of N
integers using divide and conquer. Write a main that takes the array A as input from the user and prints
the maximum sum subarray and its sum using the function find_max_sum_subarray.
9 4 −1 2 1
−2 1 −3 4 −1 2 1 −5 4 sum 6
6 6 -1 2
4 -10 6 -1 2 -3 sum 7
Pseudocode:
Page 9
Divide and conquer |Algo Lab | Fariha Tabassum Islam
Page 10
Divide and conquer |Algo Lab | Fariha Tabassum Islam
3 Alg
Algolab
Algorithms
Algeria
4 No common prefix
Algolab
Algorithms
Algeria
UIU
Page 11
Divide and conquer |Algo Lab | Fariha Tabassum Islam
Pseudocode:
Running time:
( )
Page 12
Divide and conquer |Algo Lab | Fariha Tabassum Islam
Can we achieve ?
Yes. Don't sort points in the strip from scratch each time.
● Each recursive returns two lists: all points sorted by y coordinate, and all points sorted
by x coordinate.
● Sort by merging two pre-sorted lists.
● ( )
Page 13
Divide and conquer |Algo Lab | Fariha Tabassum Islam
4. How to change quick sort for descending order? (related to problem 09)
5. How to find the minimum-sum subarray? (related to problem 10)
6. Can the furthest pair be found in O(n log n) time? (related to problem 12)
7. Can divide and conquer algorithmic problems only be solved using recursion? - Quora
Reference:
● Slides of Dr. Md. Abul Kashem Mia, Professor, CSE Dept, BUET
● Divide and conquer algorithms (article) | Khan Academy
● Counting Inversions
● Maximum subarray problem - Wikipedia
● Chapter 5 - Divide and Conquer
● CMSC 451: Closest Pair of Points
Page 14