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

Module 2 (1)

The document discusses the Divide and Conquer algorithm, detailing its three main steps: Divide, Conquer, and Merge, along with examples like Binary Search and Merge Sort. It explains how Binary Search efficiently locates an item in a sorted array and presents algorithms for finding maximum and minimum values. Additionally, it describes the Merge Sort and Quick Sort algorithms, highlighting their processes and time complexities.

Uploaded by

abrin2112
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)
10 views

Module 2 (1)

The document discusses the Divide and Conquer algorithm, detailing its three main steps: Divide, Conquer, and Merge, along with examples like Binary Search and Merge Sort. It explains how Binary Search efficiently locates an item in a sorted array and presents algorithms for finding maximum and minimum values. Additionally, it describes the Merge Sort and Quick Sort algorithms, highlighting their processes and time complexities.

Uploaded by

abrin2112
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/ 15

CA4CRT10-DESIGN & ANALYSIS OF ALGORITHMS Module 2

DIVIDE AND CONQUER


GENERAL METHOD
Divide and Conquer Algorithm is a problem-solving technique used to solve problems by dividing
the main problem into subproblems, solving them individually and then merging them to find solution to
the original.

Divide and Conquer Algorithm can be divided into three steps: Divide, Conquer and Merge.

1. Divide:
 Break down the original problem into smaller subproblems.
 Each subproblem should represent a part of the overall problem.
 The goal is to divide the problem until no further division is possible.
2. Conquer:
 Solve each of the smaller subproblems individually.
 If a subproblem is small enough (often referred to as the “base case”), we solve it directly without
further recursion.
 The goal is to find solutions for these subproblems independently.
3. Merge:
 Combine the sub-problems to get the final solution of the whole problem.
 Once the smaller subproblems are solved, we recursively combine their solutions to get the solution of
larger problem.
 The goal is to formulate a solution for the original problem by merging the results from the
subproblems.
Example:
Binary search, maximum and minimum problem, sorting – Merge sort, quick sort, selection sort, Strassen’s
matrix multiplication.
BINARY SEARCH
It is extremely efficient algorithm. This search technique searches the given item in minimum possible
comparisons. To do the binary search, first we had to sort the array elements. The logic behind this technique
is given below:

1. First find the middle element of the array.


2. Compare the mid element with an item.
3. There are 3 cases.
a. If it is desired element the search is successful
b. If it is less than desired item then searches only the first half of the array.
c. If it is greater than the desired element searches in the second half of the array.

st nd
Searched in 1 half of array mid value searched in 2 half of array

First value first+last/2 last value

Repeat the same steps until an element is found in the search area. In this algorithm every time we are
reducing the search area. So, number of comparisons keeps on decreasing.

Department of computer science 1 SAC, Peruvanthanam


CA4CRT10-DESIGN & ANALYSIS OF ALGORITHMS Module 2

Algorithm

Here a : sorted array

beg : index value of first element in the array.

end : index value of last element in the array.

mid : Middle location of segment of array “a”.

Binsearch(a, n, item)

beg =0;

end=n-1;

while(beg<=end && a[mid]!= item )

mid= (beg+end)/2;

if (item <a[mid])

end=mid-1

else

beg= mid+1

mid=int(beg+end)/2

if (a[mid]= item)

loc=mid

else

loc =0

}
For example

beg=0

end= 9

Department of computer science 2 SAC, Peruvanthanam


CA4CRT10-DESIGN & ANALYSIS OF ALGORITHMS Module 2

mid=(0+9)/2 =4

beg=0

end= 9

mid=4

Loop starts..,

0<= 9 and a[4] !=23

0<=9 and 16 !=23√

23 < a[4]

23< 16 ×

Else part works….

beg=4+1=5

beg=5

end= 9

mid=(5+9)/2 =7

beg=5

end= 9

mid=7

Department of computer science 3 SAC, Peruvanthanam


CA4CRT10-DESIGN & ANALYSIS OF ALGORITHMS Module 2

Loop starts..,

5<= 9 and a[7] !=23

5<=9 and 56!=23√

23 < a[7]

23< 56√

end=7-1=6

beg=5

end= 6

mid=(5+6)/2 =5

Loop starts..,

5<= 6 and a[5] !=23 ×

a[mid]=item

a[mid]=item

a[5]=23√

Loc=5

Time complexity

Here every time we are reducing the search area. So, number of comparisons keeps on decreasing. In worst
case the number of comparisons is at most log(N+1). So, it is an efficient algorithm when compared to linear
search but the array has to be sorted before doing binary search.

FINDING THE MAXIMUM AND MINIMUM

The problem is to find the maximum and minimum items in a set of n elements.

Algorithm:

MaxMin(a, i, j)

Department of computer science 4 SAC, Peruvanthanam


CA4CRT10-DESIGN & ANALYSIS OF ALGORITHMS Module 2

if(i==j)

return (a[i],a[j])

if(i==(j-1))

if(a[i]<a[j])

return (a[i],a[j])

else

return a[j],a[i]

else

int mid=(i+j)/2

Lmin,Lmax(MaxMin(a,i,mid);

rmin,rmax(MaxMin(a,mid+1,j);

if(Lmax>Rmax)

max=Lmax;

else

max= Rmax;

if(Lmin<Rmin)

min=Lmin;

else

min=Rmin;

return (min,max)

Where i’ is the index value of the first element in the array and ‘j’ is the index value of the last element in the
array. For finding the maximum and minimum value in an array we need to consider three cases.

Case1: If the array contains only one element.

Example :

Array a= [5]

MaxMin(a,5,5)

Department of computer science 5 SAC, Peruvanthanam


CA4CRT10-DESIGN & ANALYSIS OF ALGORITHMS Module 2

Max=min=a[0]=5;

Case 2: if the array contains two elements.

Example: array a=[50,40]

MaxMin(a,0,1)

a[1]<a[2]

50<40, false

a[1]=50 & a[0]=40

min=40 & max=50

Case 3: an array contains n elements (more than two)

Example

Department of computer science 6 SAC, Peruvanthanam


CA4CRT10-DESIGN & ANALYSIS OF ALGORITHMS Module 2

MERGE SORT
Merge sort is a sorting algorithm that follows the divide-and-conquer approach. It works by
recursively dividing the input array into smaller subarrays and sorting those subarrays then merging them
back together to obtain the sorted array.
In simple terms, we can say that the process of merge sort is to divide the array into two halves, sort each
half, and then merge the sorted halves back together. This process is repeated until the entire array is
sorted.

How it works:

1. Divide the unsorted array into two sub-arrays, half the size of the original.
2. Continue to divide the sub-arrays as long as the current piece of the array has more than one element.
3. Merge two sub-arrays together by always putting the lowest value first.
4. Keep merging until there are no sub-arrays left.

ALGORITHM

Mergesort(low,high)

if(low<high)

mid=(low+high)/2;

Mergesort(low,mid);

Mergesort(mid+1,high);

Merge(low,mid,high);

Merge(low,mid,high)

h=low;

i=low;

Department of computer science 7 SAC, Peruvanthanam


CA4CRT10-DESIGN & ANALYSIS OF ALGORITHMS Module 2

j=mid+1;

while((h<=mid)&&(j<=high))

if(a[h]<=a[j])

b[i]=a[h];

h++;

else

b[i]=a[j];

j++;

i++;

if(h>mid)

for(k=j;k<=high;k++)

b[i]=a[k];

i++;

else

for(k=h;k<=mid;k++)

b[i]=a[k];

Department of computer science 8 SAC, Peruvanthanam


CA4CRT10-DESIGN & ANALYSIS OF ALGORITHMS Module 2

i++;

for(k=low;k<=high;k++)

a[k]=b[k];

Example:

Step 1: We start with an unsorted array, and we know that it splits in half until the sub-arrays only consist
of one element. The Merge Sort function calls itself two times, once for each half of the array. That means that
the first sub-array will split into the smallest pieces first.

[ 12, 8, 9, 3, 11, 5, 4]
[ 12, 8, 9] [ 3, 11, 5, 4]
[ 12] [ 8, 9] [ 3, 11, 5, 4]
[ 12] [ 8] [ 9] [ 3, 11, 5, 4]

Step 2: The splitting of the first sub-array is finished, and now it is time to merge. 8 and 9 are the first two
elements to be merged. 8 is the lowest value, so that comes before 9 in the first merged sub-array.

[ 12] [ 8, 9] [ 3, 11, 5, 4]

Step 3: The next sub-arrays to be merged is [ 12] and [ 8, 9]. Values in both arrays are compared from the
start. 8 is lower than 12, so 8 comes first, and 9 is also lower than 12.

Department of computer science 9 SAC, Peruvanthanam


CA4CRT10-DESIGN & ANALYSIS OF ALGORITHMS Module 2

[ 8, 9, 12] [ 3, 11, 5, 4]

Step 4: Now the second big sub-array is split recursively.

[ 8, 9, 12] [ 3, 11, 5, 4]
[ 8, 9, 12] [ 3, 11] [ 5, 4]
[ 8, 9, 12] [ 3] [ 11] [ 5, 4]

Step 5: 3 and 11 are merged back together in the same order as they are shown because 3 is lower than 11.

[ 8, 9, 12] [ 3, 11] [ 5, 4]

Step 6: Sub-array with values 5 and 4 is split, then merged so that 4 comes before 5.

[ 8, 9, 12] [ 3, 11] [ 5] [ 4]
[ 8, 9, 12] [ 3, 11] [ 4, 5]

Step 7: The two sub-arrays on the right are merged. Comparisons are done to create elements in the new
merged array:

1. 3 is lower than 4
2. 4 is lower than 11
3. 5 is lower than 11
4. 11 is the last remaining value

[ 8, 9, 12] [ 3, 4, 5, 11]

Step 8: The two last remaining sub-arrays are merged. Let's look at how the comparisons are done in more
detail to create the new merged and finished sorted array:

3 is lower than 8:

Before [ 8, 9, 12] [ 3, 4, 5, 11]


After: [ 3, 8, 9, 12] [ 4, 5, 11]

Step 9: 4 is lower than 8:

Before [ 3, 8, 9, 12] [ 4, 5, 11]


After: [ 3, 4, 8, 9, 12] [ 5, 11]

Step 10: 5 is lower than 8:

Before [ 3, 4, 8, 9, 12] [ 5, 11]


After: [ 3, 4, 5, 8, 9, 12] [ 11]

Step 11: 8 and 9 are lower than 11:

Before [ 3, 4, 5, 8, 9, 12] [ 11]


After: [ 3, 4, 5, 8, 9, 12] [ 11]

Step 12: 11 is lower than 12:

Department of computer science 10 SAC, Peruvanthanam


CA4CRT10-DESIGN & ANALYSIS OF ALGORITHMS Module 2

Before [ 3, 4, 5, 8, 9, 12] [ 11]


After: [ 3, 4, 5, 8, 9, 11, 12]

The sorting is finished!

The Time Complexity of Merge Sort is O(n log n) in both the average and worst cases.

QUICK SORT

The Quick sort is developed by C.A.R Hoare. The quick sort algorithm works by partitioning the array to
be sorted. And each partition is in turn sorted recursively. In partition, one of the array elements is chosen as
a key value. This key value can be the first element of an array. That is, if a is an array then key=a[0]. And rest
of the array elements are grouped into two partitions such that

 One partition contains elements smaller that the key value


 Another partition contains elements larger that the key value.

Values < pivot


key values > pivot

0 1 2 3 4 5 6 7 8
7 6 10 5 9 2 1 15 7

Here 7 is selected as a key value. Then, two indices namely, start and end are used to indicate the element and
the last element. The low index starts on the left and selects an element that is greater than the key value.
Then these elements are interchanged. This process is repeated until all elements to the left of the key are
smaller than the key value. And all elements to the right of the key are greater than the key value.

The steps involved in placing the key value, 7 in its proper position in the array are given below. The
elements being compared are encircled on each line.

6
7 10 5 9 2 1 15 7
start
h

7 6 10 5 9 2 1 15 7 start
h start 6 <7

10 7
7 6 5 9 2 1 15 Start :10 > 7,
h start end end: 7=7

Interchange 10 and 7

7 6 7 5 9 2 1 15 10 Start: 5 < 7
h start end

Department of computer science 11 SAC, Peruvanthanam


CA4CRT10-DESIGN & ANALYSIS OF ALGORITHMS Module 2

7 6 7 5 9 2 1 15 10 Start :9>7
h start end

9 15
7 6 7 5 start
2 1 end
10 End: 15 >7
h

7 6 7 5 9 2 1 15 10
start end End :1 <7
h

Interchange 9 and 1

1 9
7 6 7 5 2 15 10
h start end

7 6 7 5 1 2 9 15 10
start end Start: 2<7
h

9
7 6 7 5 1 2 End,start
15 10 End :9>7
h

7 2 9
6 7 5 1 end start
15 10 End :2<7
h
Interchange pivot 7 with end, since start>end

2 6 7 5 1 7 9 15 10

Values <7 Values >7

The given array has been partitioned into two sub arrays. The first sub array is {2, 6, 7, 5, 1} and the second
sub array is {9, 15, 10}. We can repeatedly apply this procedure on each of these sub arrays until the entire
array is sorted. Since. Since the array elements are partitioned and exchanged, this technique is called
partition-exchange technique.

In this quick sort technique, each time the given array is partitioned into two smaller sub arrays, each
of these sub arrays can be processed either iteratively r recursively. The recursion quick sort algorithm is
presented below.

Partition (a, LB, UB)

1. Pivot = a[LB]
2. Start = LB
3. End = UB
4. Repeat steps 5 to 7 while start < end
5. Repeat while a[start] <= pivot

Department of computer science 12 SAC, Peruvanthanam


CA4CRT10-DESIGN & ANALYSIS OF ALGORITHMS Module 2

i. Start = start +1
[end of loop]

6. Repeat while a[end] > pivot


i. End = end – 1
[end of loop]

7. If start < end, then swap a[start] and a[end]


i. temp = a[start]
ii. a[start] = a[end]
iii. a[end] =temp
[end of step 4 loop]

8. Swap a[LB] with a[end]


temp = a[LB]

a[LB] = a[end]

a[end] = temp

9. Return end
ALGORITHM Quick sort (a, LB, UB)

1. If LB < UB
Loc =partition (a, LB, UB)

Quick sort (a, LB, loc-1)

Quick sort (a, loc+1, UB)

2. End
Time complexity

Worst case : O(n2)

SELECTION SORT
A Selection sort algorithm sorts an array of elements by iterating and finding the smallest/largest one. Once
it is found it is placed in the first position of array. Another remaining element is made to find the next
smallest element which is placed in the second position and so on.

Let a be a linear array of n elements.

Pass 1: find the location loc of the smallest in the list of elements.

a[1] , a[2],….,a[n], and then interchanged a[loc] and a[1].then:

Pass 2: Find the location loc of the smallest in the sub list of n-1 elements.

a[2] , a[3],….,a[n], and then interchanged a[loc] and a[2].then:

a[1], a[2] is stored , since a[1]<=a[2]

Pass 3: Find the location loc of the smallest in the sub list of n-2 elements.

a[3] , a[4],….,a[n], and then interchanged a[loc] and a[3].then:

a[1], a[2], a[3] is stored , since a[2]<=a[3]

… ………………………………………………

…. ……………………………………………….

Department of computer science 13 SAC, Peruvanthanam


CA4CRT10-DESIGN & ANALYSIS OF ALGORITHMS Module 2

Pass n-1: Find the location loc of the smaller of the elements a[n-1, a[n], and then interchanged a[loc] and
a[n-1]. Then :

a[1], a[2], a[3], ……, a[n] is stored , since a[n-1]<=a[n]

Thus a is stored after n-1 passes.

For example:

40 30 50 20 10

a[0] a[1] a[2] a[3] a[4]

k=0 loc=4 : 10 30 50 20 40

k=1 loc=3 : 10 20 50 30 40

k=2 loc=3 : 10 20 30 50 40

k=3 loc=4 : 10 20 30 40 50

Sorted list is : 10 20 30 40 50

ALGORITHM

1. Repeat steps 2 and 3 for k= 0, 1,2, ……..n-1


2. Call min (a, k, n, loc)
3. [Interchange a[k] and a[loc] ]
Set temp:= a[k]

a[k]:= a[loc]

a[loc]:=temp

[End of step 1 loop]

4. Exit
PROCEDURE min(a,k,n,loc)

1. Set min:= a[k] and loc:= k [initializes pointers]


2. Repeat for j=k+1, k+2,……..n-1;
If min>a[j], then:

set min:= a[j]

loc:= j.

[End of loop]

3. Return.
Time complexity

The selection sort makes first pass in n-1 comparisons, the second pass in n-2 comparisons and o on. The
total number of comparisons are ( (n+1)+(n+2))+….+1=n(n-1)/2

STRASSEN'S MATRIX MULTIPLICATION


Strassen's Matrix Multiplication is the divide and conquer approach to solve the matrix multiplication
problems. The usual matrix multiplication method multiplies each row with each column to achieve the
product matrix. The time complexity taken by this approach is O(n3), since it takes two loops to multiply.
Strassen’s method was introduced to reduce the time complexity from O(n3) to O(nlog 7).

Department of computer science 14 SAC, Peruvanthanam


CA4CRT10-DESIGN & ANALYSIS OF ALGORITHMS Module 2

Strassen’s Matrix multiplication can be performed only on square matrices where n is a power of 2. Order
of both of the matrices are n × n.

Divide X, Y and Z into four (n/2)×(n/2) matrices as represented below −

Department of computer science 15 SAC, Peruvanthanam

You might also like