Sei sulla pagina 1di 11

Design and analysis of algorithm

UNIT-II Divide and Conquer


Quick Sort

divide a list into big values and small values, then sort each part

✦ QuickSort is another divide-and-conquer sorting algorithm


✦ The main idea is to partition the array into two regions:
❖ small items are moved to the left side of the array
❖ large items are moved to the right side
✦ After partitioning, repeat the sort on the left and right sides
❖ each region is a sub-problem, a smaller version of the original problem
✦ Main question: how do we decide which items are “small” and which are “large”?
✦ A common technique: use the first item in the region as a pivot
❖ everything less than the pivot ends up in the left region
items greater than or equal to the pivot go in the right region
Binary Search

✦ The binary search algorithm uses the divide-and-conquer strategy


to search through an array
✦ The array must be sorted
❖ the “zeroing in” strategy for looking up a
word in the dictionary won’t work it the
words are not in alphabetical order
❖ binary search will not work unless the
array is sorted
✦ divide and conquer
❖ break a problem into smaller pieces and solve the smaller
sub-problems
✦ It may not seem like that big a deal, but the improvement can be
dramatic
❖ approximate number of comparisons (worst case):

Breaking large problems into smaller subproblems

✦ Binary Search
✦ Binary Search Experiments
✦ Merge Sort
✦ Merge Sort Experiments
✦ Recursive Methods

Binary Search

✦ The binary search algorithm uses the divide-and-conquer strategy


to search through an array
✦ The array must be sorted
❖ the “zeroing in” strategy for looking up a
word in the dictionary won’t work it the
words are not in alphabetical order
❖ binary search will not work unless the
array is sorted

Merge Sort
❖ sort subgroups of size 2, merge them into sorted groups of
size 4, merge those into sorted groups of size 8, ...

❖ The merge sort algorithm works from “the bottom up”


❖ start by solving the smallest pieces of the main problem
❖ keep combining their results into larger solutions
❖ eventually the original problem will be solved

 Merge sort is a sorting technique based on divide and conquer


technique.
 With worst- case time complexity being Ο(n log n), it is one of the
most respected algorithms.
 Merge sort first divides the array into equal halves and then
combines them in a sorted manner.
 Invented by John von Neumann (1903-1957)
 Follows divide and conquer paradigm.
 Developed merge sort for EDVAC in 1945

Methods
Divide: Divide the unsorted list into two sub lists of about half the
size.
Conquer: Sort each of the two sub lists recursively until we have list
sizes of length 1,in which case the list itself is returned.
Combine: Merge the two-sorted sub lists back into one sorted list.

Algorithm
MERGE SORT (A,p,r) //divide
if p < r then q= [ (p + r) / 2 ]
MERGE SORT(A,p,q)
MERGER SORT(A,q + 1,r)
MERGE(A,p,q,r)
Merge(array A, int p, int q, int r)
{
array B[p..r] //temp array taken
i = k = p // initialize pointers
j = q+1
while (i <= q and j <= r)
{ if (A[i] <= A[j]) B[k++] = A[i++]
else
B[k++] = A[j++]
}
while (i <= q) B[k++] = A[i++] // copy any leftover to B
while (j <= r) B[k++] = A[j++]
for i = p to r A[i] = B[i] // copy B back to A
}

Potrebbero piacerti anche