Presentation is loading. Please wait.

Presentation is loading. Please wait.

Divide and Conquer – and an Example QuickSort

Similar presentations


Presentation on theme: "Divide and Conquer – and an Example QuickSort"— Presentation transcript:

1 Divide and Conquer – and an Example QuickSort
L. Grewe

2 Divide-and-conquer technique
a problem of size n subproblem 1 of size n/2 subproblem 2 of size n/2 a solution to subproblem 1 a solution to subproblem 2 a solution to the original problem

3 Divide and Conquer Algorithms
Based on dividing problem into subproblems Divide problem into sub-problems Subproblems must be of same type Subproblems do not need to overlap Conquer by solving sub-problems recursively. If the sub-problems are small enough, solve them in brute force fashion Combine the solutions of sub-problems into a solution of the original problem (tricky part)

4 D-A-C For Divide-and-Conquer algorithms the running time is mainly affected by 3 criteria: The number of sub-instances into which a problem is split. The ratio of initial problem size to sub- problem size. The number of steps required to divide the initial instance and to combine sub-solutions.

5 An example of D-A-C Given an array of n values find, in order, the lowest two values efficiently. To make life easier we assume n is at least 2 and that it is always divisible by 2 Have you understood the problem?? The solution should be 2 followed by 4 2 10 9 6 4 5 What if?? 2 8 7 9 3

6 Possible Solutions Sort the elements and take the two first
We get more information than we require (not very efficient!!) Find the lowest, then find the second lowest Better than the first but still we have to go through the array two times Be careful not to count the lowest value twice Search through the array keeping track of the lowest two found so far Lets see how the divide and conquer technique works!

7 Divide and conquer approach
There are many ways to divide the array It seems reasonable that the division should be related to the problem Division based on 2!! Divide the array into two parts? Divide the array into pairs? Go ahead with the second option!

8 The algorithm Lets assume the following array
We divide the values into pairs We sort each pair Get the first pair (both lowest values!) 2 6 7 3 5 9 4 1 2 6 7 3 5 9 4 1 2 6 3 7 5 9 1 4

9 The algorithm (2) We compare these values (2 and 6) with the values of the next pair (3 and 7) Lowest 2,3 The next one (5 and 6) The next one (2 and 9) Lowest 2,2 The next one (1 and 4) Lowest 1,2 2 6 3 7 5 9 1 4

10 Example: Divide and Conquer
Binary Search Heap Construction Tower of Hanoi Exponentiation Fibonnacci Sequence Quick Sort Merge Sort Multiplying large Integers Matrix Multiplications Closest Pairs

11 Divide and Conquer – One Example
Quicksort Partition array into two parts around pivot Recursively quicksort each part of array Concatenate solutions 3

12 Quick Sort divide and conquer and recursive
"Divide and conquer", Caesar's other famous quote "I came, I saw, I conquered" Divide-and-conquer idea dates back to Julius Caesar. Favorite war tactic was to divide an opposing army in two halves, and then assault one half with his entire force.

13 Sorting Problem Revisited
Given: an unsorted array Goal: sort it 5 2 4 7 1 3 6 1 2 3 4 5 6 7

14 Quick Sort Approach Performance
Select pivot value (hopefully near median of list) Partition elements (into 2 lists) using pivot value Recursively sort both resulting lists Concatenate resulting lists For efficiency pivot needs to partition list evenly Performance O( n log(n) ) average case O( n2 ) worst case

15 Quick Sort Algorithm L G E If list below size K
Sort w/ other algorithm Else pick pivot x and partition S into L elements < x E elements = x G elements > x Quicksort L & G Concatenate L, E & G If not sorting in place x x L G E x

16 Quick Sort Code void QuickSort ( ItemType A[ ] , int first, int last )
// Pre: first <= last // Post: Sorts array A[ first. .last ] into // ascending order { if ( first < last ) // general case { int splitPoint; splitPoint = Split ( A, first, last) ; // A[ first ] . . A[splitPoint - 1 ] <= splitVal // A[ splitPoint ] = splitVal // A[ splitPoint + 1 ] . . A[ last ] > splitVal QuickSort( A, first, splitPoint - 1 ) ; QuickSort( A, splitPoint + 1, last ); } Lower end of array region to be sorted Upper end of array region to be sorted

17 Quick Sort Code Use first element as pivot
//PSUEDO CODE !!!! Split(A, first, last) { splitVal = A[first]; //use first element as splitVal i = first; //counter starting at first for(j=first+1 thru last) { if(A[j] <= splitval) { i= i +1; //swap new A[i] with A[j] temp = A[j]; A[j] = A[i]; A[i] = temp; } //now swap A[first](contains splitVal) with A[i] temp = A[i]; A[i] = A[first]; A[first] = A[i]; //return new splitPoint location, the location of splitVal return i; Use first element as pivot Partition elements in array relative to value of pivot Place pivot in middle of partitioned array, return index of pivot

18 Quick Sort Example 7 8 7 8 2 5 4 2 4 5 4 5 4 5 Partition & Sort Result


Download ppt "Divide and Conquer – and an Example QuickSort"

Similar presentations


Ads by Google