You are on page 1of 41

Divide – and – conquer

1
contents
✓ The General Method,
✓ Binary search
✓ finding, maximum and minimum
✓ Selection sort

✓ Quick sort
✓ Merge sort

2
What is divide and conquer algorithm?
In computer science, divide and conquer is
an algorithm design paradigm.
A divide-and-conquer algorithm recursively breaks
down a problem into two or more sub-problems of the
same or related type, until these become simple
enough to be solved directly. Next, solve every sub-
problem individually, recursively.
Finally, the solutions to the sub-problems are then
combined to give a solution to the original problem.

3
The following computer algorithms are based on divide-
and-conquer programming approach :-

• Binary Search
• Quick Sort
• Merge Sort
• Strassen's Matrix Multiplication
• Closest pair (points)

4
Binary search
• Binary search is a fast search algorithm with run-time complexity
of Ο(log n). This search algorithm works on the principle of divide
and conquer.
• Binary search is the search technique which works efficiently on
the sorted lists. Hence, in order to search an element into some
list by using binary search technique, we must ensure that the list
is sorted.
• Binary search follows divide and conquer approach in which, the
list is divided into two halves and the item is compared with the
middle element of the list. If the match is found then, the location
of middle element is returned otherwise, we search into either of
the halves depending upon the result produced through the
match.
5
Cont…

6
Cont…

7
Overview of Simple sorting algorithms
• Insertion
• Selection and
• Bubble sort

8
Insertion sort
void insert(int a[], int n) /* function to sort an aay with insertion sort */
{
int i, j, temp;
for (i = 1; i < n; i++) {
temp = a[i];
j = i - 1;
while(j>=0 && temp <= a[j])
/* Move the elements greater than temp to one position ahead from their current position*/

{
a[j+1] = a[j];
j = j-1;
}
a[j+1] = temp;
}
}
9
Insertion sort

10
11
Selection sort
void selection(int arr[], int n)
{
int i, j, small;
for (i = 0; i < n-1; i++) // One by one move boundary of unsorted subarray
{
small = i; //minimum element in unsorted array
for (j = i+1; j < n; j++)
if (arr[j] < arr[small])
small = j;
// Swap the minimum element with the first element
int temp = arr[small];
arr[small] = arr[i];
arr[i] = temp;
}
}
12
13
14
Bubble sort
void bubbleSort(int arr[], int n)
{
int i, j, temp;
for(i = 0; i < n; i++)
{
for(j = 0; j < n-i-1; j++)
{
if( arr[j] > arr[j+1])
{ // swap the elements that push largest no. to right
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}}}
15
Bubble sort

16
17
Quick sort
Quicksort is the widely used sorting algorithm that makes
O(n log n) comparisons in average case for sorting an array
of n elements and O(n2) in the worst case. It is a faster and
highly efficient sorting algorithm.
It follows the divide and conquer approach. Divide and
conquer is a technique of breaking down the algorithms
into subproblems, then solving the subproblems
recursively, and combining the results/solutions back
together to solve the original problem.
18
Cont…
• Divide: In Divide, first pick a pivot element. After that, partition
or rearrange the array into two sub-arrays such that each
element in the left sub-array is less than or equal to the pivot
element and each element in the right sub-array is larger than
the pivot element.

• Conquer: Recursively, sort two subarrays with Quicksort.

• Combine: Combine the already sorted array. 19


Choosing the pivot
• Picking a good pivot is necessary for the fast implementation
of quicksort. However, it is typical to determine a good pivot.
Some of the ways of choosing a pivot are as follows -
 Pivot can be random, i.e. select the random pivot from the
given array
Pivot can either be the rightmost element or the leftmost
element of the given array.
Select median as the pivot element.

20
Implementation algorithm:

21
Partitioning

22
Quick sort running time complexity

23
Quick sort

24
25
Worst case running time analysis of Quick Sort: when elements are sorted.

T(n)=O(n2)

 Each time the Quick Sort performs the partition() on the input array, the pivot selected is the smallest/largest
value in the array.

26
27
28
Merge Sort
• Merge sort is a sorting technique based on divide and conquer
technique. With worst-case time complexity being Ο(n log n), it is one
of the most respected algorithms.
• Merge sort keeps on dividing the list into equal halves until it can no
more be divided. By definition, if it is only one element in the list, it is
sorted. Then, merge sort combines the smaller sorted lists keeping
the new list sorted too.
• Step 1 − if it is only one element in the list it
is already sorted, return.
• Step 2 − divide the list recursively into two
halves until it can no more be divided.
• Step 3 − merge the smaller lists into new list in
sorted order.
29
merge Sort

30
// merge sort function
void mergeSort(int arr[], int l, int r)
{
int mid;
if(l < r)
{ mid = (l +( r-l)) / 2;
mergeSort(arr, l, mid);
mergeSort(arr, mid+1, r);
merge(arr, l, mid, r);
}
}
31
• // function to merge the subarrays
void merge(int arr[], int p, int q, int r) {
// Create L<--A[p..q] and M <-- A[q+1..r]
int n1 = q - p + 1;
int n2 = r - q;
int L[n1], M[n2];
for (int i = 0; i < n1; i++)
L[i] = arr[p + i];
for (int j = 0; j < n2; j++)
M[j] = arr[q + 1 + j];
// Maintain current index of sub-arrays and main array
int i, j, k;
i = 0;
j = 0;
k = p;

32
// Until we reach either end of either L or M, pick larger among
// elements L and M and place them in the correct position at A[p..r]
while (i < n1 && j < n2) {
if (L[i] <= M[j]) {
arr[k] = L[i]; // When we run out of elements in either L or M,
i++; // pick up the remaining elements and put in A[p..r]
while (i < n1) {
} else {
arr[k] = L[i];
arr[k] = M[j]; i++;
k++;
j++;
}
} while (j < n2) {
k++; arr[k] = M[j];
j++;
} k++;
}
}

33
Merge sort running time complexity

34
Summery

35
Strassen’s matrix multiplication
There are some procedures:
1.Divide a matrix of order of 2*2 recursively till we get the matrix of 2*2.
2.Use the previous set of formulas to carry out 2*2 matrix multiplication.
3.In this eight multiplication and four additions, subtraction are performed.
4.Combine the result of two matrixes to find the final product or final matrix.
Formulas for Stassen’s matrix multiplication
In Strassen’s matrix multiplication there are seven multiplication and four addition,
subtraction in total.
1.M1 = (A11 + A22) (B11 + B22)
2.M2 = (A21 + A22).B11 C11 = M1 + M4 – M5 + M7
3.M3 = (B12 – B22). A11 C12 = M3 + M5
4.M4 = (B21 – B11). A22
5.M5 = (A11 + A12).B22 C21 = M2 + M4
6.M6 = (A21 – A11) . (B11 + B12) C22 = M1 + M3 – M2 + M6
7.M7 = (A12 – A22) . (B21 + B22)
36
Master Theorem

Let f be an increasing function that such that


f( n ) = a.f( n / b ) + c.nd
whenever n = bk, where a, b, k  Z+ & c > 0 & d ≥ 0:
If ( a < bd ) then f( n ) is O( nd )
If ( a = bd ) then f( n ) is O( ndlogn )
If ( a > bd ) then f( n ) is O( nlogba ).
37
Cont…
•Using Naïve method and divide conquer
approach :
• Two matrix multiplication will take time
complexity of O(n3)
•Using Strassen’s matrix multiplication: time
complexity takes O(n2.81)

38
Closest pair algorithm: using divide and concquer
Step1: sort the points both by x and y coordinates. Need to keep separate
arrays to access the points in sorted order.
Step2: Divide the points given into two subsets pleft and pright by a vertical line
x=m so that half the points to the left or on the right or on the line.
Step3: Find recursively the closest pairs for the left and right subsets.
-Its time complexity is: O(nlogn) using DnC
- O(n2) using brute force

39
Finding minimum and maximum using DnC
• Using nave method: Using divide and conquer method:

40
Analysis
• The number of comparison in Naive method:is 2n - 2. -> O(n)
• Using Divide and conquer approach:

41

You might also like