File Organization and Processing
Lecture 9
Divide and Conquer
by: T.Aisha Alasmari
Topics
– Introduction.
– Divide and Conquer Technique.
– Applications.
– Divide and Conquer Examples.
• Mergesort.
• Quicksort.
• Efficiency.
Introduction
The most-well known algorithm general design strategy, it works as following:
1. Divide instance of problem into two or more smaller instances, ideally of about the same size.
2. Solving the sub-instances independently.
3. Combining the sub-solutions to obtain the solution of the original instance.
Divide and Conquer Technique
Divide and Conquer Technique
• Basic Steps:
– Divide: the problem into a number of subproblems that are themselves smaller instances of the same type of problem.
– Conquer: Recursively solving these subproblems. If the
subproblems are small enough, solve them straightforward.
– Combine: the solutions to the subproblems into the solution of original problem.
Applications
• Obviously application:
–
Sort a list of names.
– Organize an MP3 library.
– Display Google PageRank results.
• Non-obvious applications:
– Data compression.
– Computer graphics.
– Computational biology.
– Book recommendations on Amazon.
Divide and Conquer Examples
1. Sorting: mergesort and quicksort.
2. Binary tree traversals.
3. Binary search.
4. Multiplication of large integers.
5. Matrix multiplication: Strassen’s algorithm.
6. Closest-pair and convex-hull algorithms.
Mergesort
• Split array A[0..n-1] in two about equal halves and make copies of each half in arrays B and C.
• Sort arrays B and C recursively.
Mergesort
• Merge sorted arrays B and C into array A as follows:
– Repeat the following until no elements remain in one of the arrays:
• compare the first elements in the remaining unprocessed portions of the arrays
• copy the smaller of the two into A, while incrementing the index indicating the unprocessed portion of that array
– Once all elements in one of the arrays are
processed, copy the remaining unprocessed
elements from the other array into A.
Mergesort
8 3 2 9 7 1 5 4
8 3 2 9 7 1 5 4
8 3 2 9 7 1 5 4
8 3 2 9 7 1 5 4
3 8 2 9 1 7 4 5
2 3 8 9 1 4 5 7
1 2 3 4 5 7 8 9
Quicksort
• Unlike mergesort, which divides its input's
elements according to their position in the array.
• Quicksort divides them according to their value.
• Specifically, it rearranges elements of a given array A[0 .. n - 1] to achieve its partition, a situation where all the elements before some
position s are smaller than or equal to A[s] and all the elements after positions are greater than or
equal to A[s]
Quicksort
• Obviously, after a partition has been achieved, A[s]
will be in its final position in the sorted array, and we can continue sorting the two subarrays of the
elements preceding and following A[s] independently (e.g., by the same method).
p
A[i]p A[i]p
Quicksort
• Select an element to divide the subarray, we call this element the pivot.
• There are several different strategies for selecting a
pivot, the simplest strategy is selecting the subarray's
first element p = A[l].
Quicksort
• Two scans of the subarray each comparing the subarray's elements with the pivot. :
1. left-to-right (i) starts from 2ed one and keep small element to left; stop at first element ≥ pivot.
2. right-to-left (j), starts from last one and keep large element to right; stop at first element ≤ pivot.
Quicksort
• After both scans stop:
1. i < j (not crossed), exchange A[i] and A[j] and resume the scans by i++ and j--.
2. i ≥ j (crossed or the same element), exchange pivot with A[j] and we will have partitioned the array.
Quicksort
Efficiency
• Quicksort makes only 38% more comparisons than in the best case.
• Moreover, it is efficient as it runs faster than