2246 Divide and Conquer Concepts
2246 Divide and Conquer Concepts
General Method
In divide and conquer approach, a problem is divided into smaller problems, then
the smaller problems are solved independently, and finally the solutions of smaller
problems are combined into a solution for the large problem.
Defective Chessboard
Binary search looks for a particular item by comparing the middle most item
of the collection. If a match occurs, then the index of item is returned. If the
middle item is greater than the item, then the item is searched in the sub-array to
the left of the middle item.
2. Set two pointers low and high at the lowest and the highest positions
respectively.
3. Find the middle element mid of the array ie. arr[(low + high)/2] = 6.
4. If x == mid, then return mid.Else, compare the element to be searched with
m.
5. If x > mid, compare x with the middle element of the elements on the right side
of mid. This is done by setting low to low = mid + 1.
6. Else, compare x with the middle element of the elements on the left side of mid.
This is done by setting high to high = mid - 1.
7. Repeat steps 3 to 6 until low meets high.
8.
9. x = 4 found
Max-Min Problem
In this approach, the array is divided into two halves. Then using recursive
approach maximum and minimum numbers in each halves are found. Later, return
the maximum of two maxima of each half and the minimum of two minima of each
half.
Merge Sort
The MergeSort function keeps on splitting an array into two halves until a
condition is met where we try to perform MergeSort on a subarray of size 1, i.e., p
== r. And then, it combines the individually sorted subarrays into larger arrays
until the whole array is merged.
Quick Sort
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.
Selection Sort
Strassen, is an algorithm for matrix multiplication. It is faster than the standard matrix
multiplication algorithm for large matrices, with a better asymptotic complexity, although
the naive algorithm is often better for smaller matrices.