0% found this document useful (0 votes)
7 views

lec-2

Uploaded by

Amanuel Tamirat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

lec-2

Uploaded by

Amanuel Tamirat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

DIVIDE AND CONQUER

Wachemo University(Durame Campus)


School of Computing and Informatics
Department of Computer Science
CHAPTER TWO: DIVIDE AND CONQUER

Mr. Abraham Wolde(2024)

1
Outlines
Chapter Two: Divide And Conquer
General Method
Binary Search
Finding Maximum And Minimum
Merge Sort
Quick Sort

2
THE GENERAL METHOD

The most-well known algorithm design strategy:


1. Divide instance of problem into two or more
smaller instances
2. Solve smaller instances recursively
3. Obtain solution to original (larger) instance by
combining these solutions

3
THE GENERAL METHOD

4
THE GENERAL METHOD
Control Abstraction for Divide & Conquer
Algorithm D and C(P)
{
if Small(P) then return S(P);
else
{
divide P into smaller instances P1, P2, …,Pk, K≥1;
Apply D And C to each of these sub problems;
return Combine(D And C(p1), D And C(p2),…D And C(pk));
}
} 5
THE GENERAL METHOD
Analysis
Input size =n;
g(n) = time taken to compute the answer directly for small inputs.
f(n) = time for dividing and combing the solution to sub problems.
T(n) = g(n) n is small
T(n1)+T(n2)+………….+T(nk)+f(n) otherwise

Recurrences
The complexity of many divide-and-conquer algorithms is given by
recurrences of the form
T(n) = T(1) n=1;
aT(n/b) + f(n) n>1;
6
THE GENERAL METHOD

Applications of Dividing and Conquer: the


applications of Dividing and Conquer methods
are :-
1. Binary search
2. quick sort
3. Merge sort

7
Binary search

 It is efficient searching technique that works


with only sorted lists.

8
BINARY SEARCH
ITERATIVE BINARY SEARCH
Algorithm Bin search(a,n,x)
// Given an array a[1:n] of elements in non-decreasing
//order, n>=0,determine whether ‘x’ is present and
// if so, return ‘j’ such that x=a[j]; else return 0.
{
low:=1; high:=n;
while (low<=high) do
{
mid:=[(low+high)/2];
if (x<a[mid]) then high=mid-1;
else if(x>a[mid]) then
low=mid+1;
else return mid;
}
return 0;
} 9
Example for Binary search.

10
Cont.…

11
Cont.….

12
13
Time complexity of binary search

14
FINDING MAXIMUM AND
MINIMUM
STRAIGHT FORWARD METHOD

15
Cont…
 Straight MaxMin requires 2(n-1) element comparison in the best,
average & worst cases.
 Hence we can replace the contents of the for loop by,
 If(a[I]>max) then max:=a[I];
 Else if (a[I]<min) then min:=a[I];
 Now the best case occurs when the elements are in increasing order.
 The no. of element comparison is (n-1).
 The worst case occurs when the elements are in decreasing order.
 The no. of elements comparison is 2(n-1)
 The average no. of element comparison is < than 2(n-1)

16
17
18
Merge Sort
Merge sort is based on divide and conquer
techniques. It is a two phase process:-
1. dividing
2. merging
Dividing phase: each time the given list of elements
is divided into two parts . This division process
continues until the list is small enough to divide.
Merging phase: it is the process of combining two
sorted lists, so that, the resultant lists is also the
sorted one.
19
20
21
22
Quick Sort
The quick sort is considered to be a fast method
to sort the elements.
This method is based on divide and conquer
technique i.e the entire list is divided into various
partitions and sorting is applied again and again
on these partitions.
This method is also called as partition exchange
sorts.

23
Divide and conquer
Quicksort an n-element array:
1. Divide: Partition the array into two subarrays
around a pivot x such that elements in lower
subarray  x  elements in upper subarray.
 x x xx  x x
2. Conquer: Recursively sort the two subarrays.
3. Combine: Trivial.
Key: Linear-time partitioning subroutine.
Partitioning subroutine
PARTITION(A, p, q) ⊳A [ p . . q]
x  A[ p] ⊳pivot = A[ p]
ip
for j  p + 1 to q
do if A[ j]  x
then i  i + 1
exchange A[i]  A[ j]
exchange A[ p]  A[i]
return i

Invariant: xx  x x  x x ??
p i j q
Pseudocode for quicksort
QUICKSORT(A, p, r)
if p < r
then q  PARTITION(A, p, r)
QUICKSORT(A, p, q–1)
QUICKSORT(A, q+1, r)

Initial call: QUICKSORT(A, 1, n)


Question & Answer

27
Thank You !!!

28

You might also like