Divide and Conquer – and an Example QuickSort

Slides:



Advertisements
Similar presentations
Stephen P. Carl - CS 2421 Recursive Sorting Algorithms Reading: Chapter 5.
Advertisements

Chapter 4: Divide and Conquer Master Theorem, Mergesort, Quicksort, Binary Search, Binary Trees The Design and Analysis of Algorithms.
Quicksort Quicksort     29  9.
Efficient Sorts. Divide and Conquer Divide and Conquer : chop a problem into smaller problems, solve those – Ex: binary search.
Sorting Algorithms and Average Case Time Complexity
Updated QuickSort Problem From a given set of n integers, find the missing integer from 0 to n using O(n) queries of type: “what is bit[j]
1 Sorting Problem: Given a sequence of elements, find a permutation such that the resulting sequence is sorted in some order. We have already seen: –Insertion.
Chapter 4: Divide and Conquer The Design and Analysis of Algorithms.
(c) , University of Washington
1 Programming with Recursion. 2 Recursive Function Call A recursive call is a function call in which the called function is the same as the one making.
Order Statistics. Order statistics Given an input of n values and an integer i, we wish to find the i’th largest value. There are i-1 elements smaller.
Lecture 2 Sorting. Sorting Problem Insertion Sort, Merge Sort e.g.,
Sorting CS Sorting means... Sorting rearranges the elements into either ascending or descending order within the array. (we’ll use ascending order.)
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
Divide-and-Conquer The most-well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances 2.Solve smaller instances.
Sorting Algorithms Merge Sort Quick Sort Hairong Zhao New Jersey Institute of Technology.
PREVIOUS SORTING ALGORITHMS  BUBBLE SORT –Time Complexity: O(n 2 ) For each item, make (n –1) comparisons Gives: Comparisons = (n –1) + (n – 2)
Sorting Ordering data. Design and Analysis of Sorting Assumptions –sorting will be internal (in memory) –sorting will be done on an array of elements.
CMPT 238 Data Structures More on Sorting: Merge Sort and Quicksort.
Algorithm Design Techniques, Greedy Method – Knapsack Problem, Job Sequencing, Divide and Conquer Method – Quick Sort, Finding Maximum and Minimum, Dynamic.
Recursion.
Advanced Sorting.
Lecture 2: Divide and Conquer
Chapter 11 Sorting Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and Mount.
Sorting.
Lecture 2 Sorting.
Chapter 2 Divide-and-Conquer algorithms
Subject Name: Design and Analysis of Algorithm Subject Code: 10CS43
UNIT- I Problem solving and Algorithmic Analysis
Chapter 2 Divide-and-Conquer algorithms
Chapter 4 Divide-and-Conquer
Chapter 7 Sorting Spring 14
Algorithm Design Methods
Sorting means The values stored in an array have keys of a type for which the relational operators are defined. (We also assume unique keys.) Sorting.
Chapter 4: Divide and Conquer
Advanced Sorting Methods: Shellsort
Unit-2 Divide and Conquer
Chapter 5 Divide and Conquer
Data Structures Review Session
Topic: Divide and Conquer
Richard Anderson Lecture 13 Divide and Conquer
C++ Plus Data Structures
Yan Shi CS/SE 2630 Lecture Notes
Divide-and-Conquer The most-well known algorithm design strategy:
Sub-Quadratic Sorting Algorithms
slides adapted from Marty Stepp
Merge Sort.
Divide and Conquer Algorithms Part I
Chapter 4.
CS 3343: Analysis of Algorithms
Quicksort.
CSE 373: Data Structures and Algorithms
Sorting.
CSE 373 Data Structures and Algorithms
Algorithms: Design and Analysis
CSC 380: Design and Analysis of Algorithms
CSC 143 Java Sorting.
Chapter 10 Sorting Algorithms
Richard Anderson Lecture 14 Divide and Conquer
Searching/Sorting/Searching
CSCE 3110 Data Structures & Algorithm Analysis
CSCE 3110 Data Structures & Algorithm Analysis
Richard Anderson Lecture 14, Winter 2019 Divide and Conquer
Richard Anderson Lecture 14 Divide and Conquer
CSCE 3110 Data Structures & Algorithm Analysis
Divide and Conquer Merge sort and quick sort Binary search
Algorithm Course Algorithms Lecture 3 Sorting Algorithm-1
Quicksort.
Presentation transcript:

Divide and Conquer – and an Example QuickSort L. Grewe

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

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)

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.

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

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!

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!

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

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

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

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

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.

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

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

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

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

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

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