L14 Quicksort and Performance Optimization

Similar documents
Faster Sorting Methods

QuickSort

Quick Sort. CSE Data Structures May 15, 2002

CSC 273 Data Structures

Programming II (CS300)

Active Learning: Sorting

Data Structures and Algorithms

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

We can use a max-heap to sort data.

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

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

Homework Assignment #3. 1 (5 pts) Demonstrate how mergesort works when sorting the following list of numbers:

Real-world sorting (not on exam)

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

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

Sorting. Task Description. Selection Sort. Should we worry about speed?

CS 171: Introduction to Computer Science II. Quicksort

Unit-2 Divide and conquer 2016

CP222 Computer Science II. Searching and Sorting

Overview of Sorting Algorithms

CSE 373: Data Structures and Algorithms

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

Lecture 8: Mergesort / Quicksort Steven Skiena

Better sorting algorithms (Weiss chapter )

CS Sorting Terms & Definitions. Comparing Sorting Algorithms. Bubble Sort. Bubble Sort: Graphical Trace

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

Data Structures. Alice E. Fischer. Lecture 4, Fall Alice E. Fischer Data Structures L4... 1/19 Lecture 4, Fall / 19

CSE 373 MAY 24 TH ANALYSIS AND NON- COMPARISON SORTING

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Sorting lower bound and Linear-time sorting Date: 9/19/17

CS1 Lecture 30 Apr. 2, 2018

Sorting Algorithms. + Analysis of the Sorting Algorithms

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

4.4 Algorithm Design Technique: Randomization

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.

COSC242 Lecture 7 Mergesort and Quicksort

// walk through array stepping by step amount, moving elements down for (i = unsorted; i >= step && item < a[i-step]; i-=step) { a[i] = a[i-step];

Sorting and Searching

Algorithmic Analysis and Sorting, Part Two

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

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

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

Sorting and Selection

Question 7.11 Show how heapsort processes the input:

CS240 Fall Mike Lam, Professor. Quick Sort

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

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

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

S O R T I N G Sorting a list of elements implemented as an array. In all algorithms of this handout the sorting of elements is in ascending order

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

BM267 - Introduction to Data Structures

Table ADT and Sorting. Algorithm topics continuing (or reviewing?) CS 24 curriculum

Selection (deterministic & randomized): finding the median in linear time

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

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

Sorting. Weiss chapter , 8.6

Chapter 10. Sorting and Searching Algorithms. Fall 2017 CISC2200 Yanjun Li 1. Sorting. Given a set (container) of n elements

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

Lecture 19 Sorting Goodrich, Tamassia

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

Divide and Conquer Sorting Algorithms and Noncomparison-based

CS125 : Introduction to Computer Science. Lecture Notes #40 Advanced Sorting Analysis. c 2005, 2004 Jason Zych

Sorting Algorithms. For special input, O(n) sorting is possible. Between O(n 2 ) and O(nlogn) E.g., input integer between O(n) and O(n)

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

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

Cpt S 122 Data Structures. Sorting

Programming II (CS300)

Sorting Goodrich, Tamassia Sorting 1

Divide and Conquer CISC4080, Computer Algorithms CIS, Fordham Univ. Instructor: X. Zhang

CSE 2123 Sorting. Jeremy Morris

Divide and Conquer CISC4080, Computer Algorithms CIS, Fordham Univ. Acknowledgement. Outline

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

CHAPTER 7 Iris Hui-Ru Jiang Fall 2008

ECE 2574: Data Structures and Algorithms - Basic Sorting Algorithms. C. L. Wyatt

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

DIVIDE AND CONQUER ALGORITHMS ANALYSIS WITH RECURRENCE EQUATIONS

4. Sorting and Order-Statistics

Component 02. Algorithms and programming. Sorting Algorithms and Searching Algorithms. Matthew Robinson

Randomized Quickselect and Randomized Quicksort. Nishant Mehta September 14 th, 2017

MPATE-GE 2618: C Programming for Music Technology. Unit 4.2

Lecture 7: Searching and Sorting Algorithms

Sorting. Order in the court! sorting 1

Chapter 1 Divide and Conquer Algorithm Theory WS 2013/14 Fabian Kuhn

Quicksort. Part 3: Analyzing the Running Time

CS61BL. Lecture 5: Graphs Sorting

Sorting (Weiss chapter )

Intro to Algorithms. Professor Kevin Gold

Heaps and Priority Queues

Performance and Optimization Issues in Multicore Computing

Quick Sort. Mithila Bongu. December 13, 2013

Homework #3. CMSC351 - Spring 2013 PRINT Name :

Hiroki Yasuga, Elisabeth Kolp, Andreas Lang. 25th September 2014, Scientific Programming

Sorting (I) Hwansoo Han

ITEC2620 Introduction to Data Structures

Sorting. Order in the court! sorting 1

Recursive Sorts. Recursive Sorts. Divide-and-Conquer. Divide-and-Conquer. Divide-and-conquer paradigm:

Practical Session #11 - Sort properties, QuickSort algorithm, Selection

Algorithm for siftdown(int currentposition) while true (infinite loop) do if the currentposition has NO children then return

Introduction. two of the most fundamental concepts in computer science are, given an array of values:

! Search: find a given item in a list, return the. ! Sort: rearrange the items in a list into some. ! list could be: array, linked list, string, etc.

Sorting and Searching Algorithms

Transcription:

L14 Quicksort and Performance Optimization Alice E. Fischer Fall 2018 Alice E. Fischer L4 Quicksort... 1/12 Fall 2018 1 / 12

Outline 1 The Quicksort Strategy 2 Diagrams 3 Code Alice E. Fischer L4 Quicksort... 2/12 Fall 2018 2 / 12

Outline Review and Overview Many sort algorithms have been invented but only a few are important. They are given here in order of general efficiency, slowest to fastest. Insertion sort, (O(n 2 )) on both a linked list and an array is used to sort small numbers of items. Mergesort (guaranteed O(n log 2 (n))) works best on linked lists or as an external sort. Quicksort ( generally O(n log 2 (n))) but must be implemented well, not badly. Radix sort ( O(n k), where k is the number of fields in the data being sorted). This has a substantial setup time and is used only for very large data sets. Alice E. Fischer L4 Quicksort... 3/12 Fall 2018 3 / 12

The Quicksort Strategy Step 1. Choosing a pivot value. The pivot value defines which other data items are small and which are large. We want a pivot value that is NOT the largest or smallest data in the array. Find one by comparing the first, middle, and last values in the array. Make the median of these 3 the pivot, and put it at the beginning of the array. Put the biggest of 3 at the right end, and the smallest in the middle. The pivot and the biggest of 3 will serve as sentinel values for comparison loops. We look at first-middle-last because that is reasonably fast and because it will work well in a common situation: the array starts with sorted data and ends with data that has been added to the sorted list. Alice E. Fischer L4 Quicksort... 4/12 Fall 2018 4 / 12

The Quicksort Strategy What is a sentinel? A sentinel value is a data value placed on one end of an array or list so that a loop can process the array without constantly checking whether the end of the array has been reached. This quicksort uses two scanning pointers, starting from the ends and moving toward each other. We put the pivot at the beginning because that will serve as a sentinel value to prevent our left-moving scans from falling off the left end of the array. We put the biggest at the right end because that will serve as a sentinel value to prevent our right-moving scans from falling off the right end of the array. Using sentinels cuts the number of comparisons in half and reduces the running time considerably. Alice E. Fischer L4 Quicksort... 5/12 Fall 2018 5 / 12

The Quicksort Strategy Step 2a: partitioning the data. Each recursive call specifies the beginning and the end of the part of the array to be sorted. Divide the unsorted data in this part of the array into two sections, things that are <= the pivot value and things that are >= the pivot. Equal data items may be in either section. Start a pointer L pointing at the pivot. Start a pointer R pointing at the last value in the array. L is trying to keep the little values and get rid of the big ones. It stops scanning when a big value is found. R is trying to keep the bit values and get rid of the little ones. It stops scanning when a little value is found. Then the values under L and R must be swapped, and the scan continues. Alice E. Fischer L4 Quicksort... 6/12 Fall 2018 6 / 12

The Quicksort Strategy Step 2b: partitioning the data. Leave the scanning loop when L and R meet or cross Swap the value under R with the pivot value. This puts the pivot somewhere in the middle of the array. The pivot value is now in its sorted position: to its left, values are smaller or equal, and to its right, values are greater or equal. Now sort the left and right sections, excluding the slot now occupied by the pivot. If the number of items in a section is smaller than the recursion cutoff, use insertion sort. Otherwise, make a recursive call on quicksort. An appropriate recursion cutoff for modern machines is between 20 and 30. This recursion terminates because each recursive call will sort at least one fewer element than the prior call. Alice E. Fischer L4 Quicksort... 7/12 Fall 2018 7 / 12

Diagrams Quicksort: Getting Started Sort 12 integers using a recursion cutoff of 3. Alice E. Fischer L4 Quicksort... 8/12 Fall 2018 8 / 12

Diagrams The first Recursive Call The first recursive call returns immediately because there are only 3 items in the small part of the array. The diagram shows progress of the second recursion: Alice E. Fischer L4 Quicksort... 9/12 Fall 2018 9 / 12

Diagrams Quicksort: The Last Recursive Call The data ends up in blocks of <=3 values, separated by sorted elements. Alice E. Fischer L4 Quicksort... 10/12 Fall 2018 10 / 12

Diagrams Quicksort: Finish up using Insertion Sort When the recursions are done, we sort the small blocks of values. Alice E. Fischer L4 Quicksort... 11/12 Fall 2018 11 / 12

Code Quicksort : Code quick.hpp Quick is a template class that lets you sort an array of any base type that supports the < and > operators. You can define these operators for any C++ class. main.cpp creates a Quick object and calls its quicksort function. Alice E. Fischer L4 Quicksort... 12/12 Fall 2018 12 / 12