Unit Ii Divide and Conquer
Unit Ii Divide and Conquer
Session – 10
Syllabus
⚫ Introduction, Binary Search - Merge sort and its
algorithm analysis - Quick sort and its algorithm
analysis - Strassen's Matrix multiplication - Finding
Maximum and minimum - Algorithm for finding closest
pair - Convex Hull Problem
Introduction
⚫ Divide / Break
⚫ Conquer / Solve
⚫ Merge / Combine
Merge Sort algorithm
⚫ Merge sort is based on Divide and conquer method.
⚫ It takes the list to be sorted and divide it in half to create two unsorted lists.
⚫ The two unsorted lists are then sorted and merged to get a sorted list.
⚫ The two unsorted lists are sorted by continually calling the merge-sort algorithm; we
eventually get a list of size 1 which is already sorted. The two lists of size 1 are then
merged.
Steps using Divide and Conquer strategy
⚫ Step 1 − if it is only one element in the list it is already sorted, return.
⚫ Step 2 − divide the list recursively into two halves until it can no more be divided.
⚫ Step 3 − merge the smaller lists into new list in sorted order.
MergeSort(arr[], l, r)
If r > l
1. Find the middle point to divide the array into two halves:
middle m = (l+r)/2
Call mergeSort(arr, l, m)
Call merge(arr, l, m, r)
Mergesort(A,p,r)
Mergesort() function
MERGE-SORT (A, p, r)
1. WHILE p < r // Check for base case
2. q = FLOOR[(p + r)/2] // Divide step
3. MERGE-SORT (A, p, q)
4. MERGE-SORT (A, q + 1, r)
5. MERGE (A, p, q+1, r)