Вы находитесь на странице: 1из 4

UNIT III

Divide and conquer: General method, applications-Binary search, Quick sort, er!e sort,
"trassen#s matri$ multiplication%
Divide and conquer: General method:
1
Binary search: A binary search algorithm is a technique for finding a particular value in a sorted list. It
makes use of divide and conquer technique.
Algorithm BinarySearch
1. Sort the given array
2. Begin with an interval covering the whole array
. If the value of the search key is less than the item in the middle of the interval! narrow the interval
to the lower half. "therwise narrow it to the upper half.
#. $epeatedly check until the value is found or the interval is empty.
Quicksort is a divide and conquer algorithm. %uicksort first divides a large list into two smaller sub&lists'
the low elements and the high elements. %uicksort can then recursively sort the sub&lists.
(he steps are'
1. )ick an element! called a pivot! from the list.
2. $eorder the list so that all elements with values less than the pivot come before the pivot! while
all elements with values greater than the pivot come after it *equal values can go either way+.
After this partitioning! the pivot is in its final position. (his is called the partition operation.
. $ecursively sort the sub&list of lesser elements and the sub&list of greater elements.
(he base case of the recursion are lists of si,e ,ero or one! which never need to be sorted.
In quick sort! in every iteration! an element is chosen as pivot and elements that are less than that
pivot element are kept left to it! and elements that are greater than that pivot element are kept right to it.
Analysis of quick sort' (he best&case behavior of the quick sort algorithm occurs when in each recursion
step! the partitioning produces two parts of equal length. In order to sort n elements in this case! the
running time is "*nlog2n+. (his is because the recursion depth is log2n and on each level! there are n
elements to be treated.
(he worst case occurs! when in each recursion step an unbalanced partitioning is produced!
namely that one part consists of only one element and the other part consists of the rest of the elements
*-ig'..*c++. (hen! the recursion depth is n&1 and hence the quicksort runs in time "*n
2
+. In the average
case! a partitioning shown in -ig'..*b+ is to be e/pected. (hen! the running time would be
"*nlog2n+.(hat is! both the best&case and average&case running times are the same.
2
er!e sort:
Merge: Merging means combining two
sorted lists into one sorted list. For this,
the elements from both the sorted lists
are compared. The smaller of both the
elements is then stored in the third
array. The sorting is complete when all
the elements from both the lists are
placed in the third list.
3
"trassen#s matri$ multiplication:
(ime 0omple/ity is given by!
4

Вам также может понравиться