Quick Sort. CSE Data Structures May 15, 2002

Similar documents
Sorting Algorithms. + Analysis of the Sorting Algorithms

COMP2012H Spring 2014 Dekai Wu. Sorting. (more on sorting algorithms: mergesort, quicksort, heapsort)

Sorting. Sorting in Arrays. SelectionSort. SelectionSort. Binary search works great, but how do we create a sorted array in the first place?

CSE 332: Data Structures & Parallelism Lecture 12: Comparison Sorting. Ruth Anderson Winter 2019

IS 709/809: Computational Methods in IS Research. Algorithm Analysis (Sorting)

We can use a max-heap to sort data.

Cpt S 122 Data Structures. Sorting

CSE373: Data Structure & Algorithms Lecture 18: Comparison Sorting. Dan Grossman Fall 2013

Data Structures and Algorithms

Quicksort. Repeat the process recursively for the left- and rightsub-blocks.

SAMPLE OF THE STUDY MATERIAL PART OF CHAPTER 6. Sorting Algorithms

Mergesort again. 1. Split the list into two equal parts

Chapter 7 Sorting. Terminology. Selection Sort

Problem. Input: An array A = (A[1],..., A[n]) with length n. Output: a permutation A of A, that is sorted: A [i] A [j] for all. 1 i j n.

CS S-11 Sorting in Θ(nlgn) 1. Base Case: A list of length 1 or length 0 is already sorted. Recursive Case:

CS 171: Introduction to Computer Science II. Quicksort

CSE373: Data Structure & Algorithms Lecture 21: More Comparison Sorting. Aaron Bauer Winter 2014

Copyright 2009, Artur Czumaj 1

CSE 373: Data Structures and Algorithms

CS 137 Part 8. Merge Sort, Quick Sort, Binary Search. November 20th, 2017

Divide and Conquer Sorting Algorithms and Noncomparison-based

Sorting. Riley Porter. CSE373: Data Structures & Algorithms 1

4. Sorting and Order-Statistics

Quicksort (Weiss chapter 8.6)

Data Structures and Algorithms

Lecture 19 Sorting Goodrich, Tamassia

Sorting. Sorting. Stable Sorting. In-place Sort. Bubble Sort. Bubble Sort. Selection (Tournament) Heapsort (Smoothsort) Mergesort Quicksort Bogosort

Lecture Notes 14 More sorting CSS Data Structures and Object-Oriented Programming Professor Clark F. Olson

Computer Science 385 Analysis of Algorithms Siena College Spring Topic Notes: Divide and Conquer

Algorithms and Data Structures (INF1) Lecture 7/15 Hua Lu

QuickSort

L14 Quicksort and Performance Optimization

CS2223: Algorithms Sorting Algorithms, Heap Sort, Linear-time sort, Median and Order Statistics

Presentation for use with the textbook, Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, Merge Sort & Quick Sort

UNIT 7. SEARCH, SORT AND MERGE

Divide and Conquer 4-0

Divide-and-Conquer. Dr. Yingwu Zhu

Introduction. e.g., the item could be an entire block of information about a student, while the search key might be only the student's name

Sorting: Quick Sort. College of Computing & Information Technology King Abdulaziz University. CPCS-204 Data Structures I

Sorting (I) Hwansoo Han

Sorting Algorithms. CptS 223 Advanced Data Structures. Larry Holder School of Electrical Engineering and Computer Science Washington State University

Chapter 4. Divide-and-Conquer. Copyright 2007 Pearson Addison-Wesley. All rights reserved.

BM267 - Introduction to Data Structures

Sorting. Weiss chapter , 8.6

having any value between and. For array element, the plot will have a dot at the intersection of and, subject to scaling constraints.

DATA STRUCTURES AND ALGORITHMS

11/8/2016. Chapter 7 Sorting. Introduction. Introduction. Sorting Algorithms. Sorting. Sorting

Sorting. Data structures and Algorithms

DIVIDE AND CONQUER ALGORITHMS ANALYSIS WITH RECURRENCE EQUATIONS

Comparison Sorts. Chapter 9.4, 12.1, 12.2

Fast Bit Sort. A New In Place Sorting Technique. Nando Favaro February 2009

SORTING. Comparison of Quadratic Sorts

Analysis of Algorithms. Unit 4 - Analysis of well known Algorithms

Sorting Goodrich, Tamassia Sorting 1

Selection, Bubble, Insertion, Merge, Heap, Quick Bucket, Radix

Lecture 2: Divide&Conquer Paradigm, Merge sort and Quicksort

CSE 373: Data Structures and Algorithms

COMP Data Structures

Overview of Sorting Algorithms

CS240 Fall Mike Lam, Professor. Quick Sort

Sorting is a problem for which we can prove a non-trivial lower bound.

Quick-Sort. Quick-sort is a randomized sorting algorithm based on the divide-and-conquer paradigm:

Key question: how do we pick a good pivot (and what makes a good pivot in the first place)?

CS 61B Summer 2005 (Porter) Midterm 2 July 21, SOLUTIONS. Do not open until told to begin

Divide-and-Conquer. The most-well known algorithm design strategy: smaller instances. combining these solutions

Sorting. Bubble Sort. Pseudo Code for Bubble Sorting: Sorting is ordering a list of elements.

Module Contact: Dr Geoff McKeown, CMP Copyright of the University of East Anglia Version 1

Better sorting algorithms (Weiss chapter )

CS 171: Introduction to Computer Science II. Quicksort

08 A: Sorting III. CS1102S: Data Structures and Algorithms. Martin Henz. March 10, Generated on Tuesday 9 th March, 2010, 09:58

The divide and conquer strategy has three basic parts. For a given problem of size n,

Mergesort again. 1. Split the list into two equal parts

Chapter 1 Divide and Conquer Algorithm Theory WS 2014/15 Fabian Kuhn

Sorting Pearson Education, Inc. All rights reserved.

Sorting. Lecture10: Sorting II. Sorting Algorithms. Performance of Sorting Algorithms

Sorting. Two types of sort internal - all done in memory external - secondary storage may be used

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

O(n): printing a list of n items to the screen, looking at each item once.

Information Coding / Computer Graphics, ISY, LiTH

Programming II (CS300)

Sorting: Given a list A with n elements possessing a total order, return a list with the same elements in non-decreasing order.

Sorting and Selection

Parallel quicksort algorithms with isoefficiency analysis. Parallel quicksort algorithmswith isoefficiency analysis p. 1

Lecture #2. 1 Overview. 2 Worst-Case Analysis vs. Average Case Analysis. 3 Divide-and-Conquer Design Paradigm. 4 Quicksort. 4.

Lecture 8: Mergesort / Quicksort Steven Skiena

CSC Design and Analysis of Algorithms

17/05/2018. Outline. Outline. Divide and Conquer. Control Abstraction for Divide &Conquer. Outline. Module 2: Divide and Conquer

CS 310 Advanced Data Structures and Algorithms

ECE 242 Data Structures and Algorithms. Advanced Sorting II. Lecture 17. Prof.

CSC Design and Analysis of Algorithms. Lecture 6. Divide and Conquer Algorithm Design Technique. Divide-and-Conquer

Introduction to Computers and Programming. Today

1 a = [ 5, 1, 6, 2, 4, 3 ] 4 f o r j i n r a n g e ( i + 1, l e n ( a ) 1) : 3 min = i

Question 7.11 Show how heapsort processes the input:

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Searching, Sorting. part 1

Unit-2 Divide and conquer 2016

SORTING AND SELECTION

InserDonSort. InserDonSort. SelecDonSort. MergeSort. Divide & Conquer? 9/27/12

CSE 373: Data Structures & Algorithms More Sor9ng and Beyond Comparison Sor9ng

9/10/12. Outline. Part 5. Computational Complexity (2) Examples. (revisit) Properties of Growth-rate functions(1/3)

Transcription:

Quick Sort CSE 373 - Data Structures May 15, 2002

Readings and References Reading Section 7.7, Data Structures and Algorithm Analysis in C, Weiss Other References C LR 15-May-02 CSE 373 - Data Structures - 16 - Quick Sort 2

Sorting Ideas - swap adjacent Swap adjacent elements Bubble sort it works, but it's always slow Insertion sort works well on already sorted or partially sorted input low overhead so it works well on small inputs or as the basic sorter for a larger algorithm 15-May-02 CSE 373 - Data Structures - 16 - Quick Sort 3

Sorting Ideas - swap non-adjacent Swap non-adjacent elements Shell sort resolves multiple inversions with a single swap does an insertion sort of variable sized sub-arrays choice of increments critical Heap sort resolves multiple inversions with a single swap does insertion sort of paths through a binary heap 15-May-02 CSE 373 - Data Structures - 16 - Quick Sort 4

Sorting Ideas - recursion and merge Merging two sorted arrays is fast Partition the array and sort each part separately, then merge the results The merge can resolve many inversions with each element merged Merge sort Fast requires extra O(N) temporary array 15-May-02 CSE 373 - Data Structures - 16 - Quick Sort 5

Sorting Ideas - recursion and join Joining two sorted arrays can be very fast Partition the array into a set of little elements and a set of big elements, sort each partition, and join them The partitioning operation can move elements a long way towards the final location in one move Quick Sort Fast in-place sort (no extra space required) 15-May-02 CSE 373 - Data Structures - 16 - Quick Sort 6

Quicksort Quicksort uses a divide and conquer strategy, but does not require the O(N) extra space that MergeSort does Partition array into left and right sub-arrays the elements in left sub-array are all less than pivot elements in right sub-array are all greater than pivot Recursively sort left and right sub-arrays Concatenate left and right sub-arrays in O(1) time 15-May-02 CSE 373 - Data Structures - 16 - Quick Sort 7

Four easy steps To sort an array S If the number of elements in S is 0 or 1, then return. The array is sorted. Pick an element v in S. This is the pivot value. Partition S-{v} into two disjoint subsets, S 1 = {all values x v}, and S 2 = {all values x v}. Return QuickSort(S 1 ), v, QuickSort(S 2 ) 15-May-02 CSE 373 - Data Structures - 16 - Quick Sort 8

The steps of QuickSort S 13 81 92 43 65 31 57 26 75 0 select pivot value S 1 S 2 partition S 0 13 26 43 31 65 57 75 92 81 S 1 S 2 0 13 26 31 43 57 65 75 81 92 QuickSort(S 1 ) and QuickSort(S 2 ) S 0 13 26 31 43 57 65 75 81 92 [Weiss] Presto! S is sorted 15-May-02 CSE 373 - Data Structures - 16 - Quick Sort 9

Quicksort Example Sort the array containing: 9 16 4 15 2 5 17 1 pivot < < Partition 4 2 5 1 9 16 15 17 Partition 2 1 4 5 15 16 17 1 2 5 15 17 Concatenate 1 2 4 5 15 16 17 Concatenate 1 2 4 5 9 15 16 17 15-May-02 CSE 373 - Data Structures - 16 - Quick Sort 10

Details, details The algorithm so far lacks quite a few of the details Implementing the actual partitioning Picking the pivot want a value that will cause S 1 and S 2 to be non-zero, and close to equal in size if possible Dealing with cases where the element equals the pivot 15-May-02 CSE 373 - Data Structures - 16 - Quick Sort 11

Quicksort Partitioning Need to partition the array into left and right sub-arrays the elements in left sub-array are pivot elements in right sub-array are pivot How do the elements get to the correct partition? Choose an element from the array as the pivot Make one pass through the rest of the array and swap as needed to put elements in partitions 15-May-02 CSE 373 - Data Structures - 16 - Quick Sort 12

Partitioning is done In-Place One implementation (there are others) median3 finds pivot and sorts left, center, right Swap pivot with next to last element Set pointers i and j to start and end of array Increment i until you hit element A[i] > pivot Decrement j until you hit element A[j] < pivot Swap A[i] and A[j] Repeat until i and j cross Swap pivot (= A[N-2]) with A[i] 15-May-02 CSE 373 - Data Structures - 16 - Quick Sort 13

0 1 2 3 4 5 6 7 8 9 8 1 4 9 0 3 5 2 7 6 0 1 4 9 7 3 5 2 6 8 i j 0 1 4 9 7 3 5 2 6 8 median-of-3 pivot = 6 median3 sorts 3 elements i=left; j=right-1 i 0 1 4 9 7 3 5 2 6 8 j while(a[++i]<pivot){} i j while(a[--j]>pivot){} 0 1 4 9 7 3 5 2 6 8 i j swap(&a[i],&a[j]) 0 1 4 2 7 3 5 9 6 8 i 0 1 4 2 7 3 5 9 6 8 j while(a[++i]<pivot){} i j while(a[--j]>pivot){} 0 1 4 2 7 3 5 9 6 8 i j swap(&a[i],&a[j]) 0 1 4 2 5 3 7 9 6 8

i j 0 1 4 2 5 3 7 9 6 8 j i 0 1 4 2 5 3 7 9 6 8 j i 0 1 4 2 5 3 6 9 7 8 while(a[++i]<pivot){} while(a[--j]>pivot){} swap(&a[i],&a[right-1]) S 1 < pivot pivot S 2 > pivot

Choosing the Pivot (1) First (bad) Idea Pick the first element as pivot What if it is the smallest or largest? What if the array is sorted? How many recursive calls does quicksort make? O(N) calls, and it does O(N) work for each call, so you do O(N 2 ) work when the array is already sorted! S 1 =0 2 16 4 15 9 15-May-02 CSE 373 - Data Structures - 16 - Quick Sort 16 pivot S 2 = S -1 2 4 9 15 16 4 9 15 16 9 15 16 15 16

Choosing the Pivot (2) 2 nd (okay) Idea: Pick a random element to be the pivot Gets rid of asymmetry in left/right sizes Actually works pretty well But it requires calls to pseudo-random number generator expensive in terms of time many implementations are not particularly random 15-May-02 CSE 373 - Data Structures - 16 - Quick Sort 17

Choosing the Pivot (3a) Third idea Pick median element (N/2 th largest element) This is great it splits S exactly in two But it s hard to find the median element without sorting the entire array first, which is why we are here in the first place... 15-May-02 CSE 373 - Data Structures - 16 - Quick Sort 18

Choosing the Pivot (3b) Find the median of the first, middle and last elements - median of 3 If the data in the array is not sorted, median of 3 is similar to picking a random pivot If the data in the array is presorted, this will pick a value near the actual median of the entire array, which is good 15-May-02 CSE 373 - Data Structures - 16 - Quick Sort 19

Median-of-Three Pivot Find the median of the first, middle and last element 2 4 9 15 16 9 5 4 2 15 16 Takes only O(1) time and not error-prone like the pseudo-random 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 15-May-02 CSE 373 - Data Structures - 16 - Quick Sort 20 5

A[i]==pivot? Stop and swap while(a[++i]<pivot){} while(a[--j]>pivot){} Although this seems a little odd, it moves i and j towards the middle the benefit of balanced partitions when i and j cross in the middle outweighs the extra cost of swapping elements that are equal to the pivot 15-May-02 CSE 373 - Data Structures - 16 - Quick Sort 21

Quicksort Best Case Performance Algorithm always chooses best pivot and splits 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 plus linear time for partitioning T(N) = 2T(N/2) + O(N) Same recurrence relation as Mergesort T(N) = O(N log N) 15-May-02 CSE 373 - Data Structures - 16 - Quick Sort 22

Quicksort Worst Case Performance Algorithm always chooses the worst pivot one sub-array is 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) (see text for proof) 15-May-02 CSE 373 - Data Structures - 16 - Quick Sort 23