1 Today’s Material Divide & Conquer (Recursive) Sorting Algorithms –QuickSort External Sorting.

Slides:



Advertisements
Similar presentations
Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
Advertisements

1 Today’s Material Medians & Order Statistics – Ch. 9.
Quick Sort, Shell Sort, Counting Sort, Radix Sort AND Bucket Sort
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
Efficient Sorts. Divide and Conquer Divide and Conquer : chop a problem into smaller problems, solve those – Ex: binary search.
Quicksort CS 3358 Data Structures. Sorting II/ Slide 2 Introduction Fastest known sorting algorithm in practice * Average case: O(N log N) * Worst case:
25 May Quick Sort (11.2) CSE 2011 Winter 2011.
Quicksort COMP171 Fall Sorting II/ Slide 2 Introduction * Fastest known sorting algorithm in practice * Average case: O(N log N) * Worst case: O(N.
Chapter 7: Sorting Algorithms
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
CSC 2300 Data Structures & Algorithms March 23, 2007 Chapter 7. Sorting.
Lecture 7COMPSCI.220.FS.T Algorithm MergeSort John von Neumann ( 1945 ! ): a recursive divide- and-conquer approach Three basic steps: –If the.
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.
Insertion Sort Merge Sort QuickSort. Sorting Problem Definition an algorithm that puts elements of a list in a certain order Numerical order and Lexicographical.
Fundamentals of Algorithms MCS - 2 Lecture # 16. Quick Sort.
CS 206 Introduction to Computer Science II 12 / 09 / 2009 Instructor: Michael Eckmann.
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.
Unit 061 Quick Sort csc326 Information Structures Spring 2009.
Divide and Conquer Sorting
CSC 2300 Data Structures & Algorithms March 20, 2007 Chapter 7. Sorting.
1 7.5 Heapsort Average number of comparison used to heapsort a random permutation of N items is 2N logN - O (N log log N).
Sorting Algorithms Bubble Sort Merge Sort Quick Sort Randomized Quick Sort.
Chapter 7 (Part 2) Sorting Algorithms Merge Sort.
CS 206 Introduction to Computer Science II 12 / 08 / 2008 Instructor: Michael Eckmann.
CS2420: Lecture 11 Vladimir Kulyukin Computer Science Department Utah State University.
Sorting II/ Slide 1 Lecture 24 May 15, 2011 l merge-sorting l quick-sorting.
CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University1 Sorting - 3 CS 202 – Fundamental Structures of Computer Science II.
CSE 373 Data Structures Lecture 19
Sorting (Part II: Divide and Conquer) CSE 373 Data Structures Lecture 14.
Quick Sort By: HMA. RECAP: Divide and Conquer Algorithms This term refers to recursive problem-solving strategies in which 2 cases are identified: A case.
IKI 10100I: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100I: Data.
Computer Science 101 Fast Searching and Sorting. Improving Efficiency We got a better best case by tweaking the selection sort and the bubble sort We.
Merge Sort. What Is Sorting? To arrange a collection of items in some specified order. Numerical order Lexicographical order Input: sequence of numbers.
Quicksort, Mergesort, and Heapsort. Quicksort Fastest known sorting algorithm in practice  Caveats: not stable  Vulnerable to certain attacks Average.
COMP 171 Data Structures and Algorithms Tutorial 3 Merge Sort & Quick Sort.
1 Sorting Algorithms Sections 7.1 to Comparison-Based Sorting Input – 2,3,1,15,11,23,1 Output – 1,1,2,3,11,15,23 Class ‘Animals’ – Sort Objects.
Sorting Sorting: –Task of rearranging data in an order. –Order can be: Ascending Order: –1,2,3,4,5,6,7,8,9 Descending Order: –9,8,7,6,5,4,3,2,1 Lexicographic.
Sorting CSIT 402 Data Structures II. 2 Sorting (Ascending Order) Input ›an array A of data records ›a key value in each data record ›a comparison function.
1 CSE 373 Sorting 3: Merge Sort, Quick Sort reading: Weiss Ch. 7 slides created by Marty Stepp
1 CSE 326: Data Structures A Sort of Detour Henry Kautz Winter Quarter 2002.
Chapter 8 Sorting and Searching Goals: 1.Java implementation of sorting algorithms 2.Selection and Insertion Sorts 3.Recursive Sorts: Mergesort and Quicksort.
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: Advanced Techniques Smt Genap
1 Heapsort, Mergesort, and Quicksort Sections 7.5 to 7.7.
Decision Problems Optimization problems : minimum, maximum, smallest, largest Satisfaction (SAT) problems : Traveling salesman, Clique, Vertex-Cover,
CSE 326: Data Structures Lecture 23 Spring Quarter 2001 Sorting, Part 1 David Kaplan
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. Yet another sorting algorithm! Usually faster than other algorithms on average, although worst-case is O(n 2 ) Divide-and-conquer: –Divide:
Sorting: Implementation Fundamental Data Structures and Algorithms Margaret Reid-Miller 24 February 2004.
CS 367 Introduction to Data Structures Lecture 11.
Intro. to Data Structures Chapter 7 Sorting Veera Muangsin, Dept. of Computer Engineering, Chulalongkorn University 1 Chapter 7 Sorting Sort is.
1 Overview Divide and Conquer Merge Sort Quick Sort.
Sorting – Lecture 3 More about Merge Sort, Quick Sort.
329 3/30/98 CSE 143 Searching and Sorting [Sections 12.4, ]
Chapter 7 Sorting Spring 14
Chapter 4: Divide and Conquer
Quick Sort (11.2) CSE 2011 Winter November 2018.
slides adapted from Marty Stepp
CSE 326: Data Structures Sorting
EE 312 Software Design and Implementation I
CSE 373: Data Structures and Algorithms
CSE 373 Data Structures and Algorithms
CSE 326: Data Structures: Sorting
CSE 332: Sorting II Spring 2016.
Presentation transcript:

1 Today’s Material Divide & Conquer (Recursive) Sorting Algorithms –QuickSort External Sorting

2 Divide and Conquer Without Extra Space? Mergesort requires temporary array for merging = O(N) extra space Can we do in place sorting without extra space? Want a divide and conquer strategy that does not use the O(N) extra space Quicksort – Idea: Partition the array such that Elements in left sub- array < elements in right sub-array. Recursively sort left and right sub-arrays

3 How do we Partition the Array? Choose an element from the array as the pivot Move all elements pivot into right sub-array E.g., Suppose pivot = 7 Left subarray = 2 Right sub-array =

4 Quicksort Quicksort Algorithm: 1.Partition array into left and right sub-arrays such that: Elements in left sub-array < elements in right sub-array 2.Recursively sort left and right sub-arrays 3.Concatenate left and right sub-arrays with pivot in middle How to Partition the Array: Choose an element from the array as the pivot Move all elements pivot into right sub-array Pivot? One choice: use first element in array

5 Quicksort Example Sort the array containing: Partition < < Pivot 9 Partition Concatenate Concatenate Concatenate

6 Partitioning In Place Seems like we need an extra array for partitioning and concatenating left/right sub-arrays No! Algorithm for In Place Partitioning: 1.pivot=A[0] 2.Set pointers i and j to the beginning and the end of the array 3.Increment i until you hit an element A[i] > pivot 4.Decrement j until you hit an element A[j] < pivot 5.Swap A[i] and A[j] 6.Repeat until i and j cross (i exceeds or equals j) 7.Restore the pivot by swapping A[0] with A[j] Example: Partition in place: (pivot = A[0] = 9)

7 Partitioning In Place i Swap A[i] and A[j] j Swap i Swap A[i] and A[j] j i j Swap i ji Swap A[j] and pivot] Swap Partitioning Complete <=9 >=9

8 Partition Algorithm int Partition(int A[], int N){ if (N<=1) return 0; int pivot = A[0]; // Pivot is the first element int i=1, j=N-1; while (1){ while (A[j]>pivot) j--; // Move j while (A[i]<pivot && i<j) i++; // Move i if (i>=j) break; Swap(&A[i], &A[j]); i++; j--; } //end-while Swap(&A[j], &A[0]); // Restore the pivot return j; // return the index of the pivot } //end-Partition

9 Pivotal Role of Pivots How do we pick the pivot for each partition? –Pivot choice can make a big difference in run time 1 st Idea: Pick the first element in (sub)array as pivot –What if it is the smallest or largest? –What if the array is sorted? How many recursive calls does quicksort make? Total: O(N) recursive calls!

10 Choosing the Right Pivot 2 nd Idea: Pick a random element –Gets rid of asymmetry in left/right sizes –But…requires calls to pseudo-random number generator – expensive/error prone 3 rd idea: Pick median (N/2 largest element) Ideal but hard to compute without sorting! Compromise: Pick median of three elements

11 Median-of-Three Pivots Find the median of the first, middle and last element Takes only O(1) time and not error-prone like the pseudorandom pivot choice Less chance of poor performance as compared to looking at only 1 element For sorted inputs, splits array nicely in half each recursion Good performance

12 Partition with MedianOf3 Heuristic Before Partition Pivot = MedianOf3(42, 95, 56) = After MedianOf3 i j Need to swap 71 & 27 i j Need to swap 81 & 38 j i After Partition <= 56 >= Swap A[i]=63 & pivot= j i 5663

13 MedianOf3 Algorithm // Assumes N>=3 int MedianOf3(int A[], int N){ int left = 0; int right = N-1; int center = (left+right)/2; // Sort A[left], A[right], A[center] if (A[left] > A[center]) Swap(&A[left], &A[center]); if (A[left] > A[right]) Swap(&A[left], &A[right]); if (A[center] > A[right]) Swap(&A[center], &A[right]); Swap(&A[center], &A[right-1]); // Hide the pivot return A[right-1]; // Return the pivot } //end-MedianOf3

14 Partition with MedianOf3 // Assumes N>=3 int Partition(int A[], int N){ int pivot = MedianOf3(A, 0, N-1); int i = 0; int j = N-1; while (1){ while (A[++i] < pivot); while (A[--j] > pivot); if (i < j) Swap(&A[i], &A[j]); else break; } //end-while Swap(&A[i], &A[N-2]); // Restore the pivot return i; } //end-Partition

15 QuickSort Performance Analysis Best Case Performance: Algorithm always chooses best pivot and keeps splitting sub- arrays in half at each recursion –T(0) = T(1) = O(1) (constant time if 0 or 1 element) –For N > 1, 2 recursive calls + linear time for partitioning –Recurrence Relation for T(N) = 2T(N/2) + O(N) –Big-Oh function for T(N) = O(N logN)

16 QuickSort Performance Analysis Worst Case Performance: Algorithm keeps picking the worst pivot – one sub-array empty at each recursion –T(0) = T(1) = O(1) –T(N) = T(N-1) + O(N) –T(N-2) + O(N-1) + O(N) = … –T(0) + O(1) + … + O(N) –T(N) = O(N 2 ) Fortunately, average case performance is O(N log N)

17 External Sorting What if the data to be sorted does not fit into memory? –You have a machine with 1GB memory, but have 8GB of data to be sorted stored on a disk? We need to divide the data to several pieces, sort each piece and then merge the results 1GB 8GB unsorted data: 1GB 1GB sorted data: total 8 pieces Sort each 1GB piece separately & store on disk 8GB sorted data 8-Way Merging

18 External Sorting Running Time Assume you have N bytes to sort Further assume that your memory is M bytes There are K = N/M pieces Sorting 1 piece takes: MlogM Sorting K pieces: K*MlogM Merging K pieces: K*N Total: O(K*MlogM + K*N) = O(NlogM + N 2 /M) It is possible to perform K-way merging in N*logK time using a binary heap: Project2