02Chapter Two Divide Conquer ALGORITHM
02Chapter Two Divide Conquer ALGORITHM
i. Divide
ii. Conquer
iii. Combine
Divide and Conquer Algorithm…
i. Divide/Break
ii. Conquer/Solve
– Solve the smaller sub-problems recursively. If the sub-problem
is small enough, then solve it directly.
– Generally, at this level, the problems are considered 'solved' on their own.
iii. Combine/Merge
– When the smaller sub-problems are solved, this stage recursively
combines them until they formulate a solution of the original
problem.
– This algorithmic approach works recursively and conquer &
merge steps works so close that they appear as one.
General method of Divide and
Conquer algorithm
DAC(p){
if(small(p)){
s(p);
}
else{
Divide p into p1,p2,p3….pk
Apply DAC(p1),DAC(p2),…
Combine (DAC(p1),DAC(p2),..)
}
}
Divide and Conquer: Example #1
Let us understand this concept with the help of an
example.
• Searching Algorithms:
– Searching Algorithms are a family of algorithms used for the purpose of
searching.
– The searching of an element in the given array may be carried out in the
following two ways- Linear Search and Binary Search
Linear Search
• Linear Search:
• Consider-
• Now,
• Step-01:
– It compares element 15 with the 1st element 92.
– Since 15 ≠ 92, so required element is not found.
– So, it moves to the next element.
• Step-02:
– It compares element 15 with the 2nd element 87.
– Since 15 ≠ 87, so required element is not found.
– So, it moves to the next element.
Cont’d
Step 03 and Step 04 follows the same procedure
Step-05:
15 is present.
– In this case, the search terminates in success with just one comparison.
– The element being searched may be present at the last position or not present in
of Ο(log n).
conquer.
• Binary search halves the searchable items and thus reduces the
19
count of comparisons to be made to very less numbers.
Why Do We Need Binary Search?
• The following reasons make the binary search a better choice to be used
as a search algorithm:
list
22
Binary search Example #1
23
Solution
• First, we shall determine half of the array by using this formula −
mid = (low + high) / 2
• Here it is, 0 + (9 - 0 ) / 2 = 4 (integer value of 4.5). So, 4 is the mid of the
array.
• Now we compare the value stored at location 4, with the value being
searched, i.e. 31.
• We find that the value at location 4 is 27, which is not a match.
• As the value is greater than 27 and we have a sorted array, so we also
know that the target value must be in the upper portion of the array.
24
Solution…
• We change our low to mid + 1 and find the new mid value again.
– low = mid + 1
– mid = (low + high) / 2
– Our new mid is 7 now.
• We compare the value stored at location 7 with our target value 31.
• The value stored at location 7 is not a match, rather it is more than what
we are looking for.
• So, the value must be in the lower part from this location. 25
Solution…
26
Binary Search: Exercise #1
• Array:
50 7 11 9 25 20 27 2 51 60
27
Recursive Binary search algorithm
Algorithm RBSearch(l,h,key){
if(l=h){
if(A[l]==key) //for small element
return l;
else
return 0; //key not found
}
else{ //for large element
mid=(l+h)/2;
if(key==A[mid])
return mid;
if(key<A[mid])
return RBSearch(l,mid-1,key);
else{
return RBsearch(mid+1,h,key);
}
28
where l=low, h=high, key= elements for search
Time Complexity Analysis-Binary
Search
29
Sorting Divide-and-conquer Approach
– Quick Sort
– Selection Sort
– Insertion Sort
– Heap sort
conquer technique.
• In merge sort, the problem is divided into two sub-problems in every iteration.
– Divide break the problem into 2 sub-problem which continues until the problem
– Conquer basically merges the 2 sorted arrays into the original array
• With worst case Time complexity of merge sort is O(n log n) , it is one of the most
respected algorithms.
31
Steps in Merge Sort
• Steps:
– Divide the unsorted list into two sub lists of about half the size.
(divide)
– Sort each of the two sub lists recursively. If they are small
– Merge the two-sorted sub lists back into one sorted list (combine)
32
Merge Sort: Example #1
33
Merge Sort Algorithm
mergesort(arr[],m+1,h);
• In the worst case, in every iteration, we are dividing the problem into
further 2 sub-problems.
• Hence this will perform log n operations and this has to be done for n
iteration resulting in n log n operations total.
• In the best case that is sorted array, we can do some modification by
using a flag to check whether the element is already sorted or not
– Best Time Complexity: O(nlogn)
• Array 9 3 7 5 6 4 8 2
36
Quick Sort
Quick sort is a highly efficient sorting algorithm and is based on
partitioning of array of data into smaller arrays.
A large array is partitioned into two arrays one of which holds values
smaller than the specified value, say pivot, based on which the partition is
made and another array holds values greater than the pivot value.
i.e. It picks an element as pivot and partitions the given array around the picked
pivot.
• There are many different versions of quick sort that pick pivot in different ways:
– always pick first element as pivot, Always pick last element as pivot
– Pick a random element as pivot
37
Quicksort…
conquer strategy.
Quicksort partitions an array and then calls itself recursively twice
• It reduce the space complexity and removes the use of the auxiliary
array that is used in merge sort.
Note: This algorithm is quite efficient for large-sized data sets as
39
Quick sort: Example #1
40
Quick Sort: Example #2
• Consider an array having 8 elements
10 16 8 12 15 6 3 9 5
41
Solution
Step1. select pivot
Step2. assign first element =i and last element= j
Step3. i start from the pivot and i search for the element > pivot
Step4. j start from the last element and search for the element<pivot
Step5. increment i until>pivot and decrement j until<pivot
Step6. when i>j swap it, then j is the position of pivot
Step7. now pivot is sorted it is in partition position, so we follow the
partition position.
42
How partition is works for quick sort
Partition(A,l, h){
pivot=A[h];
i=l-1
for (j=l to h-1){
if (A[j] <= pivot)
{
i=i+1
exchange(A[i] , A[j])
}
}
exchange(A[i+1], A[h])
43
Algorithm Quicksort
Quicksort(A,l, h){
if(l<h){
j=partition(A,l,h);// j is the position where the partition is done
Quicksort(A,l,j);
Quicksort(A, j+1,h);
}
}
44
Time Complexity of Quick sort
• QuickTime Complexity
45
Quick Sort- Example #3
10 15 2 9 16 11
b.
47
How Selection Sort Works?
• Step-01: For i = 0
49
Solution….
50
Solution….
Step-05: For i = 4
• Loop gets terminated as ‘i’ becomes 4.
• The state of array after the loops are finished is as shown-
•
•
Array element:
a) 15 28 17 12 18 9 6
b) 1 4 10 8 3 7
52
Selection Sort Algorithm
54
Insertion sort
• Insertion sort- is a simple sorting algorithm that builds the final sorted array (or list)
• It is much less efficient on large lists than more advanced algorithms such as quicksort,
– Simple implementation
– Efficient for (quite) small data sets.
– Stable; i.e., does not change the relative order of elements with equal keys
– In-place; i.e., only requires a constant amount O(1) of additional memory space
– Online; i.e., can sort a list as it receives it
55
How Insertion Sort works
56
Insertion Sort: Example #1
• An insertion sort of an array of five integers
57
Insertion Sort: Example #2
77 33 44 11 88 22 66 55
• Table given below show illustration of the insertion algorithm .
• The marked elements indicates the A[K] in each pass of the
algorithm and arrow indicates the proper place for
A[K] .
58
Cont’d
59
Insertion Sort Algorithm
Algo insertionSort(A, n) {
for (int i = 1; i < (n-1) ; i ++) {
if(A[i]>A[i+1])
temp=A[i+1]
loc=i+1
A[loc]=A[loc-1]
A[loc]=loc-1
A[loc]=temp
60
}
Insertion sort analysis : Best case
61
Insertion sort analysis :Worst case
62
63