Module 2 (1)
Module 2 (1)
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:
st nd
Searched in 1 half of array mid value searched in 2 half of array
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.
Algorithm
Binsearch(a, n, item)
beg =0;
end=n-1;
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
mid=(0+9)/2 =4
beg=0
end= 9
mid=4
Loop starts..,
23 < a[4]
23< 16 ×
beg=4+1=5
beg=5
end= 9
mid=(5+9)/2 =7
beg=5
end= 9
mid=7
Loop starts..,
23 < a[7]
23< 56√
end=7-1=6
beg=5
end= 6
mid=(5+6)/2 =5
Loop starts..,
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.
The problem is to find the maximum and minimum items in a set of n elements.
Algorithm:
MaxMin(a, i, j)
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.
Example :
Array a= [5]
MaxMin(a,5,5)
Max=min=a[0]=5;
MaxMin(a,0,1)
a[1]<a[2]
50<40, false
Example
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;
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];
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.
[ 8, 9, 12] [ 3, 11, 5, 4]
[ 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:
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
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
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
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.
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
i. Start = start +1
[end of loop]
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)
2. End
Time complexity
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.
Pass 1: find the location loc of the smallest in the list of elements.
Pass 2: Find the location loc of the smallest in the sub list of n-1 elements.
Pass 3: Find the location loc of the smallest in the sub list of n-2 elements.
… ………………………………………………
…. ……………………………………………….
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 :
For example:
40 30 50 20 10
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
a[k]:= a[loc]
a[loc]:=temp
4. Exit
PROCEDURE min(a,k,n,loc)
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 can be performed only on square matrices where n is a power of 2. Order
of both of the matrices are n × n.