21/3/00SEM107- Kamin & ReddyClass 15 - Recursive Sorting - 1 Class 15 - Recursive sorting methods r Processing arrays by recursion r Divide-and-conquer.

Slides:



Advertisements
Similar presentations
Introduction to Algorithms Quicksort
Advertisements

Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
CSE 3101: Introduction to the Design and Analysis of Algorithms
Quick Sort, Shell Sort, Counting Sort, Radix Sort AND Bucket Sort
Divide and Conquer Strategy
Stephen P. Carl - CS 2421 Recursive Sorting Algorithms Reading: Chapter 5.
CSC 331: Algorithm Analysis Divide-and-Conquer Algorithms.
Chapter 4: Divide and Conquer Master Theorem, Mergesort, Quicksort, Binary Search, Binary Trees The Design and Analysis of Algorithms.
Quicksort CSE 331 Section 2 James Daly. Review: Merge Sort Basic idea: split the list into two parts, sort both parts, then merge the two lists
1 Exceptions Exceptions oops, mistake correction oops, mistake correction Issues with Matrix and Vector Issues with Matrix and Vector Quicksort Quicksort.
Spring 2015 Lecture 5: QuickSort & Selection
Introduction to Algorithms Chapter 7: Quick Sort.
QuickSort The content for these slides was originally created by Gerard Harrison. Ported to C# by Mike Panitz.
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]
Introduction to Algorithms Rabie A. Ramadan rabieramadan.org 4 Some of the sides are exported from different sources.
Quicksort Divide-and-Conquer. Quicksort Algorithm Given an array S of n elements (e.g., integers): If array only contains one element, return it. Else.
Recursive sorting: Quicksort and its Complexity
CS 162 Intro to Programming II Quick Sort 1. Quicksort Maybe the most commonly used algorithm Quicksort is also a divide and conquer algorithm Advantage.
1 Issues with Matrix and Vector Issues with Matrix and Vector Quicksort Quicksort Determining Algorithm Efficiency Determining Algorithm Efficiency Substitution.
Quick Sort. 2 Divide: Pick any element p as the pivot, e.g, the first element Partition the remaining elements into FirstPart, which contains all elements.
1 Sorting Algorithms (Part II) Overview  Divide and Conquer Sorting Methods.  Merge Sort and its Implementation.  Brief Analysis of Merge Sort.  Quick.
Quicksort. Quicksort I To sort a[left...right] : 1. if left < right: 1.1. Partition a[left...right] such that: all a[left...p-1] are less than a[p], and.
Quicksort. 2 Introduction * Fastest known sorting algorithm in practice * Average case: O(N log N) * Worst case: O(N 2 ) n But, the worst case seldom.
Quicksort.
Quicksort.
Quicksort CIS 606 Spring Quicksort Worst-case running time: Θ(n 2 ). Expected running time: Θ(n lg n). Constants hidden in Θ(n lg n) are small.
Quicksort
Sorting Algorithms Bubble Sort Merge Sort Quick Sort Randomized Quick Sort.
Chapter 7 (Part 2) Sorting Algorithms Merge Sort.
Sorting Chapter 6 Chapter 6 –Insertion Sort 6.1 –Quicksort 6.2 Chapter 5 Chapter 5 –Mergesort 5.2 –Stable Sorts Divide & Conquer.
CS2420: Lecture 11 Vladimir Kulyukin Computer Science Department Utah State University.
Introduction to Computer Science Recursive Array Programming Recursive Sorting Algorithms Unit 16.
Sorting II/ Slide 1 Lecture 24 May 15, 2011 l merge-sorting l quick-sorting.
Computer Algorithms Lecture 10 Quicksort Ch. 7 Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR.
Sorting (Part II: Divide and Conquer) CSE 373 Data Structures Lecture 14.
HKOI 2006 Intermediate Training Searching and Sorting 1/4/2006.
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.
CS 361 – Chapters 8-9 Sorting algorithms –Selection, insertion, bubble, “swap” –Merge, quick, stooge –Counting, bucket, radix How to select the n-th largest/smallest.
Sort Algorithms.
1 CSE 373 Sorting 3: Merge Sort, Quick Sort reading: Weiss Ch. 7 slides created by Marty Stepp
Quicksort CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
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.
21/3/00SEM107 - © Kamin & ReddyClass 14 - Sorting - 1 Class 14 - Review: Sorting & searching r What are sorting and searching? r Simple sorting algorithms.
Sorting: Advanced Techniques Smt Genap
Divide and Conquer Strategy
Young CS 331 D&A of Algo. Topic: Divide and Conquer1 Divide-and-Conquer General idea: Divide a problem into subprograms of the same kind; solve subprograms.
Sorting divide and conquer. Divide-and-conquer  a recursive design technique  solve small problem directly  divide large problem into two subproblems,
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)
Quicksort Quicksort is a well-known sorting algorithm that, in the worst case, it makes Θ(n 2 ) comparisons. Typically, quicksort is significantly faster.
Quicksort This is probably the most popular sorting algorithm. It was invented by the English Scientist C.A.R. Hoare It is popular because it works well.
QuickSort. Yet another sorting algorithm! Usually faster than other algorithms on average, although worst-case is O(n 2 ) Divide-and-conquer: –Divide:
Mudasser Naseer 1 3/4/2016 CSC 201: Design and Analysis of Algorithms Lecture # 6 Bubblesort Quicksort.
1 Overview Divide and Conquer Merge Sort Quick Sort.
CMPT 238 Data Structures More on Sorting: Merge Sort and Quicksort.
Quicksort "There's nothing in your head the sorting hat can't see. So try me on and I will tell you where you ought to be." -The Sorting Hat, Harry Potter.
Quicksort 1.
Chapter 4: Divide and Conquer
CO 303 Algorithm Analysis And Design Quicksort
Unit-2 Divide and Conquer
slides adapted from Marty Stepp
Quicksort.
CSE 373 Data Structures and Algorithms
Data Structures & Algorithms
Quicksort.
CSE 332: Sorting II Spring 2016.
Quicksort.
Presentation transcript:

21/3/00SEM107- Kamin & ReddyClass 15 - Recursive Sorting - 1 Class 15 - Recursive sorting methods r Processing arrays by recursion r Divide-and-conquer algorithm r Quicksort

21/3/00SEM107- Kamin & ReddyClass 15 - Recursive Sorting - 2 Processing arrays by recursion r Follow usual principle: to define f(A), assume f(B) can be calculated for any array B smaller than A. r But, instead of actually passing in a smaller array, pass A together with arguments indicating which part of A to process. r Examples: void f (double[] A, int i, int j) { // process A[i]... A[j]... recursive call f(A, i+1, j)... }

21/3/00SEM107- Kamin & ReddyClass 15 - Recursive Sorting - 3 Processing arrays by recursion (cont.) void f (double[] A, int i, int j) { // process A[i]... A[j]... recursive call f(A, i, j-1)... } void f (double[] A, int i) { // process A[i]... A[A.length-1]... recursive call f(A, i+1)... } void f (double[] A, int i) { // process A[0]... A[i]... recursive call f(A, i-1)... }

21/3/00SEM107- Kamin & ReddyClass 15 - Recursive Sorting - 4 Processing arrays by recursion (cont.) r Base cases void f (double[] A, int i, int j) { // i = j - 1-element subarray // i > j - 0-element subarray void f (double[] A, int i) { // process A[i]... A[A.length-1] // i = A.length element // i = A.length - 0-element

21/3/00SEM107- Kamin & ReddyClass 15 - Recursive Sorting - 5 Divide-and-conquer methods r Greater efficiency sometimes obtained by dividing array in half, operating on each half recursively. void f (double[] A, int i, int j) { // process subarray A[i]... A[j]... process A[i]...A[j] in linear time... f(A, i, (i+j)/2); f(A, (i+j)/2+1, j);... process A[i]...A[j] in linear time... }

21/3/00SEM107- Kamin & ReddyClass 15 - Recursive Sorting - 6 Divide-and-conquer methods pre- and post-processing: time cn time c(n/2) c(n/4) c(n/4) Total time cn cn * # of levels

21/3/00SEM107- Kamin & ReddyClass 15 - Recursive Sorting - 7 Divide-and-conquer methods for sorting r Consider subarrays of the form A[i]...A[j]. Assuming can sort arbitrary subarrays - in particular, A[i]...A[(i+j)/2] and A[(i+j)/2+1]... A[j] - how can we define divide-and- conquer sorting method? r Two “obvious” methods:

21/3/00SEM107- Kamin & ReddyClass 15 - Recursive Sorting - 8 Divide-and-conquer methods for sorting (cont.) ¬ “Post-process”: To sort A[i]...A[j]: m Sort A[i]...A[(i+j)/2] m Sort A[(i+j)/2+1]... A[j] m “Merge” sorted halves sort merge

21/3/00SEM107- Kamin & ReddyClass 15 - Recursive Sorting - 9 Divide-and-conquer methods for sorting (cont.) ­ “Pre-process”: To sort A[i]...A[j]: m Move smaller elements into left half and larger elements into right half (“partition”) m Sort A[i]... A[(i+j)/2] m Sort A[(i+j)/2+1]... A[j] partition sort

21/3/00SEM107- Kamin & ReddyClass 15 - Recursive Sorting - 10 Divide-and-conquer methods for sorting (cont.) r Method 1 called merge sort m But need to write merge step r Method 2 called quicksort m But need to write partition step

21/3/00SEM107- Kamin & ReddyClass 15 - Recursive Sorting - 11 Quicksort “Pre-process”: To sort A[i]...A[j]: m Move smaller elements into left half and larger elements into right half (“partition”) m Sort A[i]... A[(i+j)/2] m Sort A[(i+j)/2+1]... A[j] partition sort

21/3/00SEM107- Kamin & ReddyClass 15 - Recursive Sorting - 12 Quicksort (cont.) void quickSort (double[] A, int i, int j) { // sort A[i]...A[j] if ( base case )... handle base case...; else { partition(A, i, j); int midpt = (i+j)/2; quickSort(A, i, midpt); quickSort(A, midpt+1, j); } (N.B. this is the right idea, but it doesn’t work in this form. We’ll fix it in a little while.)

21/3/00SEM107- Kamin & ReddyClass 15 - Recursive Sorting - 13 Quicksort (cont.) r Only question is how to partition. r Can divide problem into two subproblems: m locating the “median” element in A[i..j], say it is A[m]. (This value is called the pivot.) m moving smaller elements than A[m] to left, larger elements to right. void partition (double[] A, int i, int j) { int pivotLoc = locOfMedian(A, i, j); swap(A,i,pivotLoc); partition1(A, i+1, j, A[i]); }

21/3/00SEM107- Kamin & ReddyClass 15 - Recursive Sorting - 14 partition1, first version  partition1(double[] A, int i, int j, double pivot) - shuffle elements of A[i..j] so that all elements less than pivot appear to the left of all elements greater than pivot. r If pivot is the median of the elements of A[i..j], then this will split the subarray exactly in half, so that the “middle” will be (i+j)/2.

21/3/00SEM107- Kamin & ReddyClass 15 - Recursive Sorting - 15 partition1, first version r Idea is simple: Suppose can partition A[i+1..j] or A[i..j-1] recursively. Two cases: m A[i] < x: Partition A[i+1..j]. m A[i] > x: Swap A[i] and A[j], then partition A[i..j-1] recursively.

21/3/00SEM107- Kamin & ReddyClass 15 - Recursive Sorting - 16 partition1, first version void partition1 (double[] A, int i, int j, double pivot) { // Shuffle elements of A[i..j] so that elements // pivot if (A[i]<pivot) … … write this! }

21/3/00SEM107- Kamin & ReddyClass 15 - Recursive Sorting - 17 Finding the median r Next problem: find median of A[i..j]. r Can easily do it in quadratic time, but how can it be done in linear time?

21/3/00SEM107- Kamin & ReddyClass 15 - Recursive Sorting - 18 Guessing the median r Unfortunately, it can’t. Second best solution: guess the median and partition around the guess, hoping for the best. r Major point: Since we can’t be sure our guess will be correct, we don’t know where the “middle” will end up. Therefore, partition1 needs to return an integer giving the location of the dividing line between small and large values.

21/3/00SEM107- Kamin & ReddyClass 15 - Recursive Sorting - 19 Quicksort again r Without the ability to divide the array exactly in half, need to change structure of quicksort somewhat: void quickSort (double[] A, int i, int j) { int m; if (i < j) { m = partition(A, i, j); quickSort(A, i, m-1); quickSort(A, m+1, j); }

21/3/00SEM107- Kamin & ReddyClass 15 - Recursive Sorting - 20 Quicksort again (cont.) r Difference is that partition has to tell quicksort where it ended up splitting the array: int partition (double[] A, int i, int j) { // Guess median of A[i]... A[j], // and move other elements so that // A[i]... A[m-1] are all less than A[m] and // A[m+1]... A[j] are all greater than A[m] // m is returned to the caller swap(A, i, guessMedianLocation(A, i, j)); int m = partition1(A, i+1, j, A[i]); swap(A, i, m); return m; }

21/3/00SEM107- Kamin & ReddyClass 15 - Recursive Sorting - 21 partition1 int partition1 (double[] A, int i, int j, double pivot) { // Shuffle elements of A[i..j] so that elements // pivot if ( base case )... if (A[i] <= pivot) // A[i] in correct half return partition1(A, i+1, j, pivot); else if (A[j] > pivot) // A[j] in correct half return partition1(A, i, j-1, pivot); else { // A[i] and A[j] in wrong half swap(A, i, j); return partition1(A, i, j-1, pivot); } r partition1 must return the “middle”.

21/3/00SEM107- Kamin & ReddyClass 15 - Recursive Sorting - 22 partition1 (cont.) r Need to handle base cases. However, there is one key point to remember here: the index returned by partition1 must contain a value less than the pivot, because it will be swapped back into A[i]. Base case needs to guarantee this: // Base case for partition1: if (j == i) if (A[i] < pivot) return i; else return i-1;

21/3/00SEM107- Kamin & ReddyClass 15 - Recursive Sorting - 23 Guessing the median r Making a good guess concerning the median is very important. r Here is a simple approach: int guessMedianLocation (double[] A, int i, int j) { return (i+j)/2; }

21/3/00SEM107- Kamin & ReddyClass 15 - Recursive Sorting - 24 Final words on quicksort r Quicksort runs in time n log n, but only if the partitions are (roughly) in half. r Worst case performance for quicksort is quadratic, but it is difficult to find examples where it is inefficient.