You are on page 1of 92

CHAPTER TWO:

DIVIDE AND CONQUER

Hawassa University

Compiled by: Kassawmar M. March, 2023 1


The General Method of Divide and Conquer
 In divide and conquer method, a given problem is,
• Divided into smaller sub problems

• These sub problems are solved independently

• Combining all the solutions of sub problems into a solution of the whole.

 If the sub problems are large enough then divide and conquer is reapplied.

 The generated sub problems are usually of same type as the original
problem. Hence recursive algorithms are used in divided and conquer
strategy.
 This method follows top-dawn approach.
 It solve decision problems. The obtained solution may or not optimal solution. 2
The General Method of Divide and Conquer 3

 Divide
• The problem into a number of sub problems
 Conquer
• The sub problems by solving them recursively. If the sub problem sizes are
small enough, however, just solve the sub problems in a straightforward
manner.

 Combine
• The solutions to the sub problems into the solution for the original problem.
3
Divide-and-Conquer: General Method
4

a problem of size n

sub problem 1 sub problem 2


of size n/2 of size n/2

a solution to a solution to
sub problem 1 sub problem 2

a solution to It general leads to a recursive


the original problem algorithm!

4
Divide-and-Conquer Examples 5

 Binary search

 Finding maximum and minimum

 Merge sort

 Quicksort

 Selection sort

 Stassen's Matrix Multiplication

5
Binary search
• Binary Search is an efficient searching method and an example of divide
and conquer approach.
• While searching the elements using this method the most essential thing is
that the elements in the array should be sorted one.
• Strategy:
1. Obtain the middle element
2. If it equals searched value, algorithm stops.
3. Other wise, two cases possible.
i. If search value < middle element, repeat the procedure for sub
array before the middle element.
ii. If search value > middle element, repeat the procedure for sub
array after the middle element.
Recursive Code for Binary search
int binarysearch (int a[], int Find, int first, int last)
{ int middle;
if(first>last) \\ not sorted
return 0;
else
{ middle=( first + last) / 2;
if(a[middle] == Find)
return middle;
else if(Find<a[middle])
return binarysearch(a, Find, first, middle-1);
else
return binarysearch(a, Find, middle+1, last);
7
}}
Binary search - example

8
8
Finding Maximum and Minimum
 Finding max and min is another example of divide and conquer problem. 9
 Problem Description: Finding min, max elements recursively.
 Max and Min algorithm.

9
9
Finding Maximum and Minimum
10
Example1: Consider a list of some elements from which maximum and
minimum element can be found out.

1 2 3 4 5 6 7 8 9
50 40 -5 -9 45 90 65 25 75

1 2 3 4 5
50 40 -5 -9 45 Sublist 1

6 7 8 9
90 65 25 75 Sublist 2

 We have divided the original list at mid and two sublists: sublist1 and
sublist2 are created. 10
Finding Maximum and Minimum
11
• Again divide each sublist and create further sublists. Then from each sublist
obtain.
1 2 3 4 5
50 40 -5 -9 45 Sublists

6 7 8 9
90 65 25 75 Sublists

• It is possible to divide the list (50,40,-5) further. Hence we have divided the
list into sublists and min, max values are obtained.
1 2 3 4 5
50 40 -5 -9 45

6 7 8 9
90 65 25 75 11
Finding Maximum and Minimum
12
• Now further division of the list is not possible. Hence we start combining
the solutions of min and max values from each sublist.
1 2 3
50 40 -5

• Combine (1,2) and (3) Min= -5, Max=50


1 2 3 4 5
50 40 -5 -9 45

• Now we will combine (1,… 3) and (4,5) and the min and max values among
them are obtained. Hence,
Min=-9 and Max=50
12
Finding Maximum and Minimum
13
6 7 8 9
90 65 25 75

• Combine (6,7) and (8,9) Min=25, Max=90


1 2 3 4 5 6 7 8 9
50 40 -5 -9 45 90 65 25 75

• Combine the sublists (1,… 5) and (6, … 9). Min=-9, Max=90


• Thus the complete list is formed from which the min and max values are
obtained. Hence final min and max values are
Min=-9 and Max=90

13
Finding Maximum and Minimum

14
Merge Sort 15
Sorting Problem: Sort a sequence of n elements into non-decreasing
order.

• Divide: Divide the n-element sequence to be sorted into two


subsequences of n/2 elements each

• Conquer: Sort the two subsequences recursively using merge sort.

• Combine: Merge the two sorted subsequences to produce the sorted


answer.

15
Merge sort Algorithm 16
 Split array A[0..n-1] into about equal halves and make copies of each half
in arrays B and C.
 Sort arrays B and C recursively.
 The dividing process ends when we have split the subsequences down to a
single item.
 A sequence of length one is trivially sorted.
 The total running time of Merge Sort is O(nlogn).
 Merge Sort is stable sorting algorithm.
 An execution of Merge Sort can be depicted by a binary tree as shown in
the next example.
16
Merge Sort Algorithm
int MergeSort(int first, int last)
{ int middle;
if(first < last) \\ at least two elements
middle = (first + last)/2;
MergeSort(first, middle);
MergeSort(middle + 1, last);
MergeSort(first, middle, last);
}
}
17
Merge Sort Example
A[] = {38, 27,43,3,9,82,10}
Then sort this by merge sort

18
Merge Sort Analysis

Best Case : O(n log2n)


Average Case : O(n log2n)
Worst Case : O(n log2n)

19
Quick Sort 20
 Sorting Problem: Sort a sequence of n elements into increasing order.

 Divide: Partition the array A[p…r] into two sub arrays A[p…q-1] and
A[q+1…r] such that each element of A[p…q-1] is less than or equal to A[q],
which is, in turn, less than or equal to each element of A[q+1…r].
• Need to find index q to partition the array

20
Quick Sort

• Conquer: Sort the two sub arrays A[p…q-1] and A[q+1…r] by


recursive calls to quick sort.

• Combine: Since the sub arrays are sorted in place, no work is needed
to combine them: the entire array A[p…r] is now sorted.

21
Quick sort Algorithm
Given an array of n elements (e.g., integers):
 If array only contains one element,
return the element and it is sorted.
 Else
• pick one element to use as pivot.
• Partition elements into two sub-arrays:
o Elements less than or equal to pivot
o Elements greater than pivot
• Quick sort two sub-arrays
• Return results
22
Quick Sort Routine
void quicksort(int a[], int l, int r)
{
int s;
if(l<r)
{
s=partition(a, l, r); // divide into two part by the pivot element we selected
quicksort(a, l, s-1);
quicksort(a, s+1, r);
}
}

23
Recursive Code for Quick Sort
int partition(int a[],int left,int right)
If(i<j)//i hold grater than pivot
{
{ //j hold less than pivot
int p,i,j,t;
t=a[i];
i=left+1;
a[i]=a[j];
j=right;
a[j]=t;
p=a[left];
}
while(j>=i)// i and j interchange
}
{
t=a[left];
while(a[i]<=p)
a[left]=a[j];
i++;
a[j]=t;
while(a[j]>p)
return j;
j--; 24
}
Example on quick sort
Pick Pivot Element
• There are a number of ways to pick the pivot element.
• In this example, we will use the first element in the array as pivot:

40 20 10 80 60 50 7 30 100

25
Partitioning Array
Given a pivot, partition the elements of the array such that the resulting array
consists of:
1. One sub-array that contains elements >= pivot
2. Another sub-array that contains elements < pivot

The sub-arrays are stored in the original data array.

26
We have 3 operations:
1. Swap left and right when l > r
2. Swap pivot and right when
pivot > right

pivot_index = 0 40 20 10 80 60 50 7 30 100
Pivot = 40
[0] [1] [2] [3] [4] [5] [6] [7] [8]

left right

27
1. While data[left] <= data[pivot_index]
++left

pivot_index = 0 40 20 10 80 60 50 7 30 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

left right

28
1. While data[left] <= data[pivot_index]
++left

pivot_index = 0 40 20 10 80 60 50 7 30 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

left right

29
1. While data[left] <= data[pivot_index]
++left

pivot_index = 0 40 20 10 80 60 50 7 30 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

left right

30
1. While data[left] <= data[pivot_index]
++left
2. While data[right] > data[pivot_index]
--right

pivot_index = 0 40 20 10 80 60 50 7 30 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

left right

31
1. While data[left] <= data[pivot_index]
++left
2. While data[right] > data[pivot_index]
--right

pivot_index = 0 40 20 10 80 60 50 7 30 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

left right

32
1. While data[left] <= data[pivot_index]
++left
2. While data[right] > data[pivot_index]
--right
3. If left< right
swap data[left] and data[right]

pivot_index = 0 40 20 10 80 60 50 7 30 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

left right

33
1. While data[left] <= data[pivot_index]
++left
2. While data[right] > data[pivot_index]
--right
3. If left< right
swap data[left] and data[right]

pivot_index = 0 40 20 10 30 60 50 7 80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

left right

34
1. While data[left] <= data[pivot_index]
++left
2. While data[right] > data[pivot_index]
--right
3. If left< right
swap data[left] and data[right]
4. While right > left, go to 1.

pivot_index = 0 40 20 10 30 60 50 7 80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

left right

35
1. While data[left] <= data[pivot_index]
++left
2. While data[right] > data[pivot_index]
--right
3. If left< right
swap data[left] and data[right]
4. While right > left, go to 1.

pivot_index = 0 40 20 10 30 60 50 7 80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

left right

36
1. While data[left] <= data[pivot_index]
++left
2. While data[right] > data[pivot_index]
--right
3. If left< right
swap data[left] and data[right]
4. While right > left, go to 1.

pivot_index = 0 40 20 10 30 60 50 7 80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

left right

37
1. While data[left] <= data[pivot_index]
++left
2. While data[right] > data[pivot_index]
--right
3. If left< right
swap data[left] and data[right]

pivot_index = 0 40 20 10 30 60 50 7 80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

left right

38
1. While data[left] <= data[pivot_index]
++left
2. While data[right] > data[pivot_index]
--right
3. If left< right
swap data[left] and data[right]
4. While right > left, go to 1.

pivot_index = 0 40 20 10 30 7 50 60 80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

left right

39
1. While data[left] <= data[pivot_index]
++left
2. While data[right] > data[pivot_index]
--right
3. If left< right
swap data[left] and data[right]
4. While right > left, go to 1.

pivot_index = 0 40 20 10 30 7 50 60 80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

left right

40
1. While data[left] <= data[pivot_index]
++left
2. While data[right] > data[pivot_index]
--right
3. If left< right
swap data[left] and data[right]
4. While right > left, go to 1.

pivot_index = 0 40 20 10 30 7 50 60 80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

left right

41
1. While data[left] <= data[pivot_index]
++left
2. While data[right] > data[pivot_index]
--right
3. If left< right
swap data[left] and data[right]
4. While right > left, go to 1.

pivot_index = 0 40 20 10 30 7 50 60 80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

left right

42
1. While data[left] <= data[pivot_index]
++left
2. While data[right] > data[pivot_index]
--right
3. If left< right
swap data[left] and data[right]
4. While right > left, go to 1.

pivot_index = 0 40 20 10 30 7 50 60 80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

left right

43
1. While data[left] <= data[pivot_index]
++left
2. While data[right] > data[pivot_index]
--right
3. If left< right
swap data[left] and data[right]
4. While right > left, go to 1 otherwise go to 5
5. Swap data[right] and data[pivot_index]

pivot_index = 0 40 20 10 30 7 50 60 80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

left right

44
1. While data[left] <= data[pivot_index]
++left
2. While data[right] > data[pivot_index]
--right
3. If left< right
swap data[left] and data[right]
4. While right > left, go to 1 otherwise go to 5
5. Swap data[right] and data[pivot_index]

pivot_index = 4 7 20 10 30 40 50 60 80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

left right

45
Partition Result

pivot_index = 4 7 20 10 30 40 50 60 80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

<= data[pivot] > data[pivot]

46
Recursion: Quicksort Sub-arrays

7 20 10 30 40 50 60 80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

<= data[pivot] > data[pivot]

47
Selection sort
1. Start with the 1st element, scan the entire list to find its smallest element
and exchange it with the 1st element.

2. Start with the 2nd element, scan the remaining list to find the smallest
among the last (N-1) elements and exchange it with the 2nd element.

3. Continue this until the entire array is sorted.

48
Selection sort
 Algorithm
• Search through the list and find the smallest element.
• Swap the smallest element with the first element.
• Repeat starting at second element and find the second smallest
element.
• Continue this until the entire array is sorted.
49
Code for Selection Sort
void selection_sort(int a[], int n)
{
int i, j, min, tmp;
for(i=0; i<=n-2; i++)
{
min=i;
for( j =i+1; j <=n-1; j++)
{
if(a[ j ] < a[min])
min = j;
}

50
Code for Selection Sort
if(min!=i)
{
tmp=a[i];
a[i]=a[min];
a[min]=tmp;
}
}
}

51
Selection Sort - EXAMPLE

52
Selection Sort: Example

5 1 3 4 6 2

Comparison

Data Movement

Sorted
53
Selection Sort

5 1 3 4 6 2

Comparison

Data Movement

Sorted
54
Selection Sort

5 1 3 4 6 2

Comparison

Data Movement

Sorted
55
Selection Sort

5 1 3 4 6 2

Comparison

Data Movement

Sorted
56
Selection Sort

5 1 3 4 6 2

Comparison

Data Movement

Sorted
57
Selection Sort

5 1 3 4 6 2

Comparison

Data Movement

Sorted
58
Selection Sort

5 1 3 4 6 2

Comparison

Data Movement

Sorted
59
Selection Sort

5 1 3 4 6 2

Smallest

Comparison

Data Movement

Sorted
60
Selection Sort

5 1 3 4 6 2

Comparison

Data Movement

Sorted
61
Selection Sort

1 5 3 4 6 2

Comparison

Data Movement

Sorted
62
Selection Sort

1 5 3 4 6 2

Comparison

Data Movement

Sorted
63
Selection Sort

1 5 3 4 6 2

Comparison

Data Movement

Sorted
64
Selection Sort

1 5 3 4 6 2

Comparison

Data Movement

Sorted
65
Selection Sort

1 5 3 4 6 2

Comparison

Data Movement

Sorted
66
Selection Sort

1 5 3 4 6 2

Comparison

Data Movement

Sorted
67
Selection Sort

1 5 3 4 6 2

Smallest

Comparison

Data Movement

Sorted
68
Selection Sort

1 5 3 4 6 2

Comparison

Data Movement

Sorted
69
Selection Sort

1 2 3 4 6 5

Comparison

Data Movement

Sorted
70
Selection Sort

1 2 3 4 5 6

Comparison

Data Movement

Sorted
71
Selection Sort

1 2 3 4 6 5

Comparison

Data Movement

Sorted
72
Selection Sort

1 2 3 4 6 5

Comparison

Data Movement

Sorted
73
Selection Sort

1 2 3 4 6 5

Comparison

Data Movement

Sorted
74
Selection Sort

1 2 3 4 6 5

Smallest

Comparison

Data Movement

Sorted
75
Selection Sort

1 2 3 4 6 5

Comparison

Data Movement

Sorted
76
Selection Sort

1 2 3 4 6 5

Comparison

Data Movement

Sorted
77
Selection Sort

1 2 3 4 6 5

Comparison

Data Movement

Sorted
78
Selection Sort

1 2 3 4 6 5

Comparison

Data Movement

Sorted
79
Selection Sort

1 2 3 4 6 5

Comparison

Data Movement

Sorted
80
Selection Sort

1 2 3 4 6 5

Smallest

Comparison

Data Movement

Sorted
81
Selection Sort

1 2 3 4 6 5

Comparison

Data Movement

Sorted
82
Selection Sort

1 2 3 4 6 5

Comparison

Data Movement

Sorted
83
Selection Sort

1 2 3 4 6 5

Comparison

Data Movement

Sorted
84
Selection Sort

1 2 3 4 6 5

Comparison

Data Movement

Sorted
85
Selection Sort

1 2 3 4 6 5

Smallest

Comparison

Data Movement

Sorted
86
Selection Sort

1 2 3 4 6 5

Comparison

Data Movement

Sorted
87
Selection Sort

1 2 3 4 5 6

Comparison

Data Movement

Sorted
88
Selection Sort

1 2 3 4 5 6

Comparison

Data Movement

Sorted
89
Selection Sort

1 2 3 4 5 6
DONE!
Comparison

Data Movement

Sorted
90
Selection Sort Analysis

• In all cases, the entire 2 loops run and comparison is made


• So, the time complexity for all three cases is same.

Time Complexity
Best Case : O(n2)
Average Case : O(n2)
Worst Case : O(n2)

91
1
-
9 92
2

You might also like