Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt

Slides:



Advertisements
Similar presentations
Lecture 3: Randomized Algorithm and Divide and Conquer II: Shang-Hua Teng.
Advertisements

Data Structures and Algorithms (AT70.02) Comp. Sc. and Inf. Mgmt. Asian Institute of Technology Instructor: Dr. Sumanta Guha Slide Sources: CLRS “Intro.
Data Structures and Algorithms (AT70.02) Comp. Sc. and Inf. Mgmt. Asian Institute of Technology Instructor: Dr. Sumanta Guha Slide Sources: CLRS “Intro.
Data Structures and Algorithms (AT70.02) Comp. Sc. and Inf. Mgmt. Asian Institute of Technology Instructor: Dr. Sumanta Guha Slide Sources: CLRS “Intro.
Data Structures and Algorithms (AT70.02) Comp. Sc. and Inf. Mgmt. Asian Institute of Technology Instructor: Dr. Sumanta Guha Slide Sources: CLRS “Intro.
QuickSort 4 February QuickSort(S) Fast divide and conquer algorithm first discovered by C. A. R. Hoare in If the number of elements in.
Quicksort CS 3358 Data Structures. Sorting II/ Slide 2 Introduction Fastest known sorting algorithm in practice * Average case: O(N log N) * Worst case:
© 2004 Goodrich, Tamassia QuickSort1 Quick-Sort     29  9.
Quick-Sort     29  9.
Tutorial 4 The Quicksort Algorithm. QuickSort  Divide: Choose a pivot, P Form a subarray with all elements ≤ P Form a subarray with all elements > P.
Analysis of Algorithms Dilemma: you have two (or more) methods to solve problem, how to choose the BEST? One approach: implement each algorithm in C, test.
Chapter 7 Quicksort Ack: This presentation is based on the lecture slides from Hsu, Lih- Hsing, as well as various materials from the web.
Data Structures and Algorithms (AT70.02) Comp. Sc. and Inf. Mgmt. Asian Institute of Technology Instructor: Prof. Sumanta Guha Slide Sources: CLRS “Intro.
Data Structures and Algorithms (AT70.02) Comp. Sc. and Inf. Mgmt. Asian Institute of Technology Instructor: Prof. Sumanta Guha Slide Sources: CLRS “Intro.
QuickSort (Ch. 7) Like Merge-Sort, based on the three-step process of divide- and-conquer. Input: An array A[1…n] of comparable elements, the starting.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 7.
Data Structures and Algorithms (AT70.02) Comp. Sc. and Inf. Mgmt. Asian Institute of Technology Instructor: Prof. Sumanta Guha Slide Sources: CLRS “Intro.
Data Structures and Algorithms (AT70.02) Comp. Sc. and Inf. Mgmt. Asian Institute of Technology Instructor: Prof. Sumanta Guha Slide Sources: CLRS “Intro.
CSC317 1 Quicksort on average run time We’ll prove that average run time with random pivots for any input array is O(n log n) Randomness is in choosing.
Data Structures and Algorithms (AT70.02) Comp. Sc. and Inf. Mgmt. Asian Institute of Technology Instructor: Prof. Sumanta Guha Slide Sources: CLRS “Intro.
Data Structures and Algorithms (AT70.02) Comp. Sc. and Inf. Mgmt. Asian Institute of Technology Instructor: Prof. Sumanta Guha Slide Sources: CLRS “Intro.
329 3/30/98 CSE 143 Searching and Sorting [Sections 12.4, ]
Data Structures and Algorithms (AT70.02) Comp. Sc. and Inf. Mgmt. Asian Institute of Technology Instructor: Prof. Sumanta Guha Slide Sources: CLRS “Intro.
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
Order Statistics Comp 122, Spring 2004.
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
Divide-And-Conquer-And-Combine
Quick-Sort 9/13/2018 1:15 AM Quick-Sort     2
Chapter 7 Sorting Spring 14
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
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.
Sorting We have actually seen already two efficient ways to sort:
Advance Analysis of Algorithms
CSC 413/513: Intro to Algorithms
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
Sorting We have actually seen already two efficient ways to sort:
ENEE641: Mathematical Foundations for Computer Engineering Home page:
Quick Sort (11.2) CSE 2011 Winter November 2018.
CO 303 Algorithm Analysis And Design Quicksort
Ch 7: Quicksort Ming-Te Chi
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
Lecture 3 / 4 Algorithm Analysis
Lecture No 6 Advance Analysis of Institute of Southern Punjab Multan
Divide-And-Conquer-And-Combine
Order Statistics Comp 550, Spring 2015.
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
CSE 373 Data Structures and Algorithms
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
Sub-Quadratic Sorting Algorithms
CS 583 Analysis of Algorithms
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
EE 312 Software Design and Implementation I
Quick-Sort 2/25/2019 2:22 AM Quick-Sort     2
Tomado del libro Cormen
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
Chapter 7 Quicksort.
Chapter 9: Medians and Order Statistics
Quick-Sort 4/25/2019 8:10 AM Quick-Sort     2
Data Structures & Algorithms
Order Statistics Comp 122, Spring 2004.
Quicksort and Randomized Algs
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
Sorting We have actually seen already two efficient ways to sort:
Data Structures and Algorithms CS 244
Presentation transcript:

Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt Data Structures and Algorithms (AT70.02) Comp. Sc. and Inf. Mgmt. Asian Institute of Technology Instructor: Prof. Sumanta Guha Slide Sources: CLRS “Intro. To Algorithms” book website (copyright McGraw Hill) adapted and supplemented

CLRS “Intro. To Algorithms” Ch. 7: Quicksort

pivot From i +1 to j is a window of elements > x = A[r]. The cursor j moves right one step at a time. If the cursor j “discovers” an element ≤ x, then this element is swapped with the front element of the window, effectively moving the window right one step; if it discovers an element > x, then the window simply becomes longer one unit.

Do Ex. 7.1-1.

Performance of Quicksort Worst-case partitioning: one subproblem of size n-1, other 0. Time: (n2). Why? Best-case partitioning: each subproblem of size at most n/2. Time: (nlog n). Why? Balanced partitioning: even if each subproblem size is at least a constant proportion of the original problem the running time is (nlog n).

Expected Running Time of RANDOMIZED-QUICKSORT Lemma 7.1: Let X be the number of comparisons performed in line 4 of PARTITION over the entire execution of QUICKSORT (or RANDOMIZED-QUICKSORT) on an n-element array. Then the running time of QUICKSORT (or RANDOMIZED-QUICKSORT) is O(n +X). Proof: There are at most n calls to PARTITION (Why? Because the pivot is dropped from future recursive calls.). Each call to PARTITION does constant work and executes the for loop some number of iterations. Therefore, the total work done is O(n) + the total number of iterations of the for loop over the entire execution of QUICKSORT. However, each iteration of the for loop performs the comparison of line 4 exactly once. Conclusion follows.

Rename the array z1, z2, …, zn, where z1≤ z2 ≤ … ≤ zn Let the indicator random variable Xij = I{zi is compared to zj}. Then X = ∑i=1..n-1 ∑j=i+1..n Xij Therefore, E[X] = E[ ∑i=1..n-1 ∑j=i+1..n Xij ] = ∑i=1..n-1 ∑j=i+1..n Pr{zi is compared to zj} Now, zi and zj are compared iff the first element to be chosen from Zij = [zi, zi+1, …, zj] as pivot is either zi or zj. Prior to the point when an element of Zij is chosen as pivot, all of Zij is in the same partition. Because pivots are chosen randomly any element of Zij is equally likely to be the first chosen as pivot. We conclude: Pr{zi is compared to zj} = 2 / (j-i+1) Therefore, E[X] = ∑i=1..n-1 ∑j=i+1..n 2 / (j-i+1) = ∑i=1..n-1 ∑k=1..n-i 2 / (k+1) ≤ ∑i=1..n-1 ∑k=1..n-1 2/(k+1) < ∑i=1..n-1 ∑k=1..n 2/k = ∑i=1..n-1 O(log n) = O(nlog n) where k = j-i ∑k=1..n 1/k ≈ loge n

Hoare’s Original Partitioning Strategy Do Prob. 7-1a

Tail Recursion Tail recursive binary search int bsNonTR(int Low, int High, double X, double A[]) { int Mid; start: if (Low > High) return (-1); //search failed Mid = (Low + High)/2; if (A[Mid] < X) Low = Mid + 1; goto start; } else if (X < A[Mid]) High = Mid - 1; else return (Mid); // X has been located int bsTR(int Low, int High, double X, double A[]) { int Mid; if (Low > High) return (-1); //search failed Mid = (Low + High)/2; if (A[Mid] < X) return ( bsTR(Mid + 1, High, X, A) ); // tail-recursive call else if (X < A[Mid]) return ( bsTR(Low, Mid - 1, X, A) ); // tail-recursive call else return (Mid); // X has been located } Tail recursive binary search A recursive call immediately before a routine exits is tail recursive. Tail recursion is wasteful as it can be replaced by iteration thereby saving on the recursion stack. Optimized compilers automatically eliminate it. Non-tail recursive binary search

Avoiding Tail Recursion in QUICKSORT The second recursive call in QUICKSORT is tail recursive and can be eliminated Tail recursion question: is the recursive Fact call tail recursive? int Fact(int n) { if (n < 1) return 1; else return n*Fact(n-1); }

Problems Ex. 7.2-3 Ex. 7.2-4 Ex. 7.3-2 Ex. 7.4-5 The running time of quicksort can be improved in practice by taking advantage of the fast running time of insertion sort when its input is “nearly” sorted. When quicksort is called on a subarray with fewer than k elements, let it simply return without sorting the subarray. After the top-level call to quicksort returns, run insertion sort on the entire array to finish the sorting process. Argue that this sorting algorithm runs in O(nk +n lg(n/k)) expected time. How should k be picked, both in theory and in practice?