Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

Chapter Two

Divide and conquer


A divide and conquer algorithm is a strategy of solving a large problem by
1. Breaking the problem into smaller sub-problems
2. Solving the sub-problems, and
3. Combining them to get the desired output.

How Divide and Conquer Algorithms Work?


Here are the steps involved:
1. Divide: Divide the given problem into sub-problems using recursion.
2. Conquer: Solve the smaller sub-problems recursively. If the subproblem is small enough, then
solve it directly.
3. Combine: Combine the solutions of the sub-problems that are part of the recursive process to
solve the actual problem.
 Assume we split the input into two sub-problems of the same kind as the original
problem.
First: If the input size of the problem is small, it is solved directly.
Second: If the input size of the problem is large, apply the strategy:
1. Divide: divide the input data S in two disjoint subsets S1and S2
2. Conquer : Solve each half of the sub-problems associated with S1 and S2
3. Merge : combine the solution for S1and S2 into a solution for S

Applications of Divide and Conquer Algorithm


The applications of the Divide and Conquer algorithm are as follows:

 Merge Sort
 Binary Search
 Quick Sort

MERGE SORT
Merge sort is one of the most efficient sorting algorithms. It is based on the divide-and-conquer
strategy. Merge sort continuously cuts down a list into multiple sub-lists until each has only one
item, and then merges those sub-lists into a sorted list.
Merge sort algorithm divides the array into two halves and applies merge sort algorithm to each
half individually after which the two sorted halves are merged together. Thus its method of
sorting is called merging.
Merge sort first divides the array into equal halves and then combines them in a sorted manner.

Let us understand this concept with the help of an example.


Here, we will sort an array using the divide and conquer approach (ie. merge sort Top-down
Approach).
1. Let the given array be

2. Divide the array into two halves


 Recursively split each subpart again into two parts until you have separate elements.

Again, divide each subpart recursively into two halves until you get individual elements.

3. Now, combine the individual elements in a sorted manner. Here, conquer and combine steps
go side by side.
Example 0ne of Divide and Conquer algorithm
A classic example of Divide and Conquer is Merge Sort demonstrated below.

In Merge Sort, we divide array into two halves, sort the two halves recursively, and then merge
the sorted halves.

How Merge Sort Works?

To understand merge sort, we take an unsorted array as the following −

We know that merge sort first divides the whole array iteratively into equal halves unless the
atomic values are achieved. We see here that an array of 8 items is divided into two arrays of size
4.
This does not change the sequence of appearance of items in the original. Now we divide these
two arrays into halves.

We further divide these arrays and we achieve atomic value which can no more be divided.

Now, we combine them in exactly the same manner as they were broken down. Please note the
color codes given to these lists.

We first compare the element for each list and then combine them into another list in a sorted
manner. We see that 14 and 33 are in sorted positions. We compare 27 and 10 and in the target
list of 2 values we put 10 first, followed by 27. We change the order of 19 and 35 whereas 42 and
44 are placed sequentially.

In the next iteration of the combining phase, we compare lists of two data values, and merge
them into a list of found data values placing all in a sorted order.

After the final merging, the list should look like this −

Now we should learn some programming aspects of merge sorting.

Quick Sort
Quicksort is a highly efficient sorting technique that divides a large data array into smaller ones.

Quick sort is capable of sorting a list of data elements significantly faster (twice or thrice faster)
than any of the common sorting algorithms.

It is an algorithm of Divide & Conquer type.


 Divide: Rearrange the elements and split arrays into two sub-arrays and an element in
between search that each element in left sub array is less than or equal to the average
element and each element in the right sub- array is larger than the middle element.
 Conquer: Recursively, sort two sub arrays.
 Combine: Combine the already sorted array.
Step by Step Process
In Quick sort algorithm, partitioning of the list is performed using following steps...
Steps 1 - Consider the first element of the list as pivot (i.e., Element at first position in the list).
Step 2 - Define two variables i and j. Set i and j to first and last elements of the list respectively.
Step 3 - Increment i until list[i] > pivot then stop.
Step 4 - Decrement j until list[j] < pivot then stop.
Step 5 - If i < j then exchange list[i] and list[j].
Step 6 - Repeat steps 3,4 & 5 until i > j.
Step 7 - Exchange the pivot element with list[j] element.
Binary Search
Binary Search is a searching algorithm used in a sorted array by repeatedly dividing the search
interval in half. The idea of binary search is to use the information that the array is sorted and
reduce the time complexity to O(Log n).
A binary search is an advanced type of search algorithm that finds and fetches data from a sorted
list of items.

Binary Search Algorithm


Binary search algorithm finds a given element in a list of elements with O(log n) time
complexity where n is total number of elements in the list. The binary search algorithm can be
used with only a sorted list of elements. That means the binary search is used only with a list of
elements that are already arranged in an order. The binary search cannot be used for a list of
elements arranged in random order. This search process starts comparing the search element with
the middle element in the list. If both are matched, then the result is "element found". Otherwise,
we check whether the search element is smaller or larger than the middle element in the list. If
the search element is smaller, then we repeat the same process for the left sublist of the middle
element. If the search element is larger, then we repeat the same process for the right sublist of
the middle element. We repeat this process until we find the search element in the list or until we
left with a sub list of only one element. And if that element also doesn't match with the search
element, then the result is "Element not found in the list".
Binary search is implemented using following steps...

Steps 1 - Read the search element from the user.


Step 2 - Find the middle element in the sorted list.
Step 3 - Compare the search element with the middle element in the sorted list.
Step 4 - If both are matched, then display "Given element is found!!!" and terminate the
function.
Step 5 - If both are not matched, then check whether the search element is smaller or larger
than the middle element.
Step 6 - If the search element is smaller than middle element, repeat steps 2, 3, 4 and 5 for the
left sub list of the middle element.
Step 7 - If the search element is larger than middle element, repeat steps 2, 3, 4 and 5 for the
right sub list of the middle element.
Step 8 - Repeat the same process until we find the search element in the list or until sublist
contains only one element.
Step 9 - If that element also doesn't match with the search element, then display "Element is
not found in the list!!!" and terminate the function.

Example one
Consider the following list of elements and the element to be searched...
Example Two

Working of Binary Search


Let us understand the working of a binary search algorithm with the help of an example.

Consider a sorted array having 10 elements as shown:


Suppose we wish to search 38 in the array.

Step 1: Find the middle element of the array.


Index (Middle) = index (low) + index (high – low)/2.
Here, middle = 0 + (9-0)/2 = 4 i.e. the element at the 4th index i.e. 25.

Step 2: Compare 38 with the middle element. 38 > 25 so, discard the first half.

Step 3: Select the send half of the array. For the second half, low = middle+1 as shown:

Step 4: Find the middle element of this smaller array which comes out to 32. Compare 38 with
32.
38 > 32 Thus, discard the first half of this array.
Step 5: Select the remaining half of the array by doing low = middle+1 as shown:

Finally, we have found 38 and thus the algorithm will halt here.

Output: 8.

Example three
The above image illustrates the following:

A. You have an array of 10 digits, and the element 59 needs to be found.


B. All the elements are marked with the index from 0 – 9. Now, the middle of the array is
calculated. To do so, you take the left and rightmost values of the index and divide them
by 2. The result is 4.5, but we take the floor value. Hence the middle is 4.
C. The algorithm drops all the elements from the middle (4) to the lowest bound because 59
is greater than 24, and now the array is left with 5 elements only.
D. Now, 59 is greater than 45 and less than 63. The middle is 7. Hence the right index value
becomes middle – 1, which equals 6, and the left index value remains the same as before,
which is 5.
E. At this point, you know that 59 come after 45. Hence, the left index, which is 5, becomes
mid as well.
F. These iterations continue until the array is reduced to only one element, or the item to be
found becomes the middle of the array.
Difference between Quick Sort and Merge Sort in tabular form
Quick sort Merge Sort
Quick Sort is a sorting algorithm mostly Merge Sort is a sorting algorithm mostly
preferred for arrays preferred for linked list
There no such need to divide the array of In merge sort, the array of elements are divided
elements into two parts (n/2) into two parts (n/2)
Quicksort algorithm can work with a small size Merge sort algorithm can work with any sizes
of datasets of datasets, small or large
This algorithm requires minimum space This algorithm requires more space

Quick Sort algorithm doesn’t require any extra Merge sort requires extra storage space to store
storage the auxiliary array
It can be said as an unstable algorithm yet it It is stable as the elements are divided into two
can be made stable by modifying some equal parts and the value is the same in sorted
changes in the code itself array as it was in unsorted array before sorting

You might also like