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

Divide and Conquer: Binary Search

1) The document discusses several divide and conquer algorithms including binary search, quicksort, merge sort, finding the closest pair of points, and Strassen's matrix multiplication algorithm. 2) Binary search recursively divides a sorted array in half to search for a target value, quicksort recursively divides an array into smaller subarrays sorted around a pivot value, and merge sort recursively divides an array in half and merges the sorted halves. 3) Finding the closest pair of points problem can be solved in quadratic time but divide and conquer provides a more efficient cubic time solution, and Strassen's algorithm multiplies matrices faster than the simple cubic time approach.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views

Divide and Conquer: Binary Search

1) The document discusses several divide and conquer algorithms including binary search, quicksort, merge sort, finding the closest pair of points, and Strassen's matrix multiplication algorithm. 2) Binary search recursively divides a sorted array in half to search for a target value, quicksort recursively divides an array into smaller subarrays sorted around a pivot value, and merge sort recursively divides an array in half and merges the sorted halves. 3) Finding the closest pair of points problem can be solved in quadratic time but divide and conquer provides a more efficient cubic time solution, and Strassen's algorithm multiplies matrices faster than the simple cubic time approach.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Divide and Conquer

Following are some standard algorithms that are Divide and Conquer algorithms.
1) Binary Search is a searching algorithm. In each step, the algorithm compares the input element x
with the value of the middle element in array. If the values match, return the index of middle.
Otherwise, if x is less than the middle element, then the algorithm recurs for left side of middle
element, else recurs for right side of middle element.
2) Quicksort is a sorting algorithm. The algorithm picks a pivot element, rearranges the array
elements in such a way that all elements smaller than the picked pivot element move to left side of
pivot, and all greater elements move to right side. Finally, the algorithm recursively sorts the subarrays
on left and right of pivot element.
3) Merge Sort is also a sorting algorithm. The algorithm divides the array in two halves, recursively
sorts them and finally merges the two sorted halves.
4) Closest Pair of Points The problem is to find the closest pair of points in a set of points in x-y
plane. The problem can be solved in O(n^2) time by calculating distances of every pair of points and
comparing the distances to find the minimum. The Divide and Conquer algorithm solves the problem
in O(nLogn) time.
5) Strassens Algorithm is an efficient algorithm to multiply two matrices. A simple method to multiply
two matrices need 3 nested loops and is O(n^3). Strassens algorithm multiplies two matrices in
O(n^2.8974) time.

Binary Search
int binary_search(int A[], int key, int imin, int imax)
{
// test if array is empty
if (imax < imin)
// set is empty, so return value showing not found
return KEY_NOT_FOUND;
else
{
// calculate midpoint to cut set in half
int imid = midpoint(imin, imax);

// three-way comparison
if (A[imid] > key)
// key is in lower subset
return binary_search(A, key, imin, imid - 1);
else if (A[imid] < key)
// key is in upper subset
return binary_search(A, key, imid + 1, imax);
else
// key has been found
return imid;
}
}

Analysis
Binary Search can be accomplished in logarithmic time in the worst case ,
i.e., T(n) = (log
in the best case.

n). This version of the binary search takes logarithmic time

Iterative Version of Binary Search


int binary_search(int A[], int key, int imin, int imax)
{
// continue searching while [imin,imax] is not empty
while (imax >= imin)
{
// calculate the midpoint for roughly equal partition
int imid = midpoint(imin, imax);
if(A[imid] == key)
// key found at index imid
return imid;
// determine which subarray to search
else if (A[imid] < key)
// change min index to search upper subarray
imin = imid + 1;
else
// change max index to search lower subarray

imax = imid - 1;
}
// key was not found
return KEY_NOT_FOUND;
}

Analysis
The analysis of Iterative algorithm is identical to that of its recursive counterpart.

You might also like