CS 144 Advanced C++ Programming May 9 Class Meeting

Slides:



Advertisements
Similar presentations
Practice Quiz Question
Advertisements

Quick Sort, Shell Sort, Counting Sort, Radix Sort AND Bucket Sort
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 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
ISOM MIS 215 Module 7 – Sorting. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.
Quicksort CS 3358 Data Structures. Sorting II/ Slide 2 Introduction Fastest known sorting algorithm in practice * Average case: O(N log N) * Worst case:
CS 206 Introduction to Computer Science II 04 / 28 / 2009 Instructor: Michael Eckmann.
25 May Quick Sort (11.2) CSE 2011 Winter 2011.
Data Structures Data Structures Topic #13. Today’s Agenda Sorting Algorithms: Recursive –mergesort –quicksort As we learn about each sorting algorithm,
CS 201 Data Structures and Algorithms Text: Read Weiss, § 7.7
Ver. 1.0 Session 5 Data Structures and Algorithms Objectives In this session, you will learn to: Sort data by using quick sort Sort data by using merge.
CS 206 Introduction to Computer Science II 12 / 09 / 2009 Instructor: Michael Eckmann.
Data Structures Advanced Sorts Part 2: Quicksort Phil Tayco Slide version 1.0 Mar. 22, 2015.
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.
Quicksort
Algorithm Efficiency and Sorting
 2003 Prentice Hall, Inc. All rights reserved Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
Sorting CS-212 Dick Steflik. Exchange Sorting Method : make n-1 passes across the data, on each pass compare adjacent items, swapping as necessary (n-1.
© 2006 Pearson Addison-Wesley. All rights reserved10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University1 Sorting - 3 CS 202 – Fundamental Structures of Computer Science II.
 2006 Pearson Education, Inc. All rights reserved Searching and Sorting.
CS 146: Data Structures and Algorithms July 14 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
Sorting (Part II: Divide and Conquer) CSE 373 Data Structures Lecture 14.
CS 46B: Introduction to Data Structures July 7 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
1 Time Analysis Analyzing an algorithm = estimating the resources it requires. Time How long will it take to execute? Impossible to find exact value Depends.
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.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
Chapter 18: Searching and Sorting Algorithms. Objectives In this chapter, you will: Learn the various search algorithms Implement sequential and binary.
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
CS 146: Data Structures and Algorithms July 14 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
Computer Science 101 Fast Algorithms. What Is Really Fast? n O(log 2 n) O(n) O(n 2 )O(2 n )
CS 146: Data Structures and Algorithms July 9 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
CS 46B: Introduction to Data Structures July 2 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
Data Structures - CSCI 102 Selection Sort Keep the list separated into sorted and unsorted sections Start by finding the minimum & put it at the front.
ICS201 Lecture 21 : Sorting King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
Searching Topics Sequential Search Binary Search.
PREVIOUS SORTING ALGORITHMS  BUBBLE SORT –Time Complexity: O(n 2 ) For each item, make (n –1) comparisons Gives: Comparisons = (n –1) + (n – 2)
Intro. to Data Structures Chapter 7 Sorting Veera Muangsin, Dept. of Computer Engineering, Chulalongkorn University 1 Chapter 7 Sorting Sort is.
CS 154 Formal Languages and Computability May 5 Class Meeting Department of Computer Science San Jose State University Spring 2016 Instructor: Ron Mak.
Sorts, CompareTo Method and Strings
COMP 53 – Week Seven Big O Sorting.
Quicksort
Chapter 7 Sorting Spring 14
CMPE Data Structures and Algorithms in C++ November 9 Class Meeting
Description Given a linear collection of items x1, x2, x3,….,xn
Quicksort 1.
Quicksort and Mergesort
Quick Sort (11.2) CSE 2011 Winter November 2018.
Unit-2 Divide and Conquer
Ch 7: Quicksort Ming-Te Chi
CMPE 180A Data Structures and Algorithms in C++ April 19 Class Meeting
CS 201 Fundamental Structures of Computer Science
Algorithm Efficiency and Sorting
CMPE 135: Object-Oriented Analysis and Design November 27 Class Meeting Department of Computer Engineering San Jose State University Fall 2018 Instructor:
Sub-Quadratic Sorting Algorithms
EE 312 Software Design and Implementation I
Quicksort.
CS 1114: Sorting and selection (part two)
CSE 373 Data Structures and Algorithms
CSC 380: Design and Analysis of Algorithms
CSC 380: Design and Analysis of Algorithms
Quicksort.
Algorithmic complexity
Design and Analysis of Algorithms
CSE 332: Sorting II Spring 2016.
CS 144 Advanced C++ Programming March 21 Class Meeting
CS 144 Advanced C++ Programming April 30 Class Meeting
CS 144 Advanced C++ Programming April 30 Class Meeting
Quicksort.
Presentation transcript:

CS 144 Advanced C++ Programming May 9 Class Meeting Department of Computer Engineering San Jose State University Spring 2019 Instructor: Ron Mak www.cs.sjsu.edu/~mak

Final Exam Friday, May 17 Similar in format to the midterm. 7:15 – 9:30 AM in MH 233 Similar in format to the midterm. Canvas + lockdown browser Multiple-choice + short-answer questions Covers the entire semester with emphasis on the second half. Tip: Be sure to understand the programming assignments.

Introduction to Move Semantics As we’ve seen, excessive calls to the copy constructor can hurt performance. In a move from source to target, instead of copying a resource, the target “steals” the resource from the source. The source contains a pointer to an object. The target gets a copy of the pointer. The source’s pointer is set to null. Therefore, the target has taken ownership of the object.

Introduction to Move Semantics, cont’d Move semantics involve lvalues and rvalues. An lvalue is an object. So called because it can appear on the left side of an assignment statement (i.e., it can be an assignment target). An rvalue is a temporary value or a value not associated with an object. So called because it can be a value that’s calculated on the right side of an assignment statement.

Introduction to Move Semantics, cont’d When an rvalue is assigned in an assignment statement, or when an rvalue is passed by value to a function, a copy is made of the value. But it is wasteful to copy a temporary value. That value normally is about to disappear. Move semantics allows us to use that temporary value by taking ownership of its resources. Reduce the amount of runtime copying.

Move Semantics Example Message.h class Message { private:     int length;     char *text;      public:     Message() : length(0), text(nullptr) {}     Message(char *t) : length(strlen(t)), text(t) {}     virtual ~Message()     {         if (length > 0) delete[] text;         length = 0;         text = nullptr;     }

Move Semantics Example, cont’d     Message(const Message& other)     {         cout << "*** Copy constructor called!" << endl;         length = other.length;         text = new char[length + 1];         strcpy(text, other.text);     }     Message(Message&& other)         cout << "*** Move constructor called!" << endl;         text = other.text;         other.length = 0;         other.text = nullptr; Copy constructor Copy the source’s data. Move constructor Steal the source’s data without copying it.

Move Semantics Example, cont’d Message& operator =(const Message& other) {     cout << "*** Copy assignment called!" << endl;     if (this != &other)     {         if (length > 0) delete[] text;         length = other.length;         text = new char[length + 1];         strcpy(text, other.text);     }     return *this; } Copy assignment Message& operator =(Message&& other) {     cout << "*** Move assignment called!" << endl;     if (this != &other)     {         if (length > 0) delete[] text;         length = other.length;         text = other.text;                      other.length = 0;         other.text = nullptr;     }     return *this; } Move assignment Steal the source’s data without copying it.

Move Semantics Example, cont’d friend ostream& operator <<(ostream& outs, const Message& msg) {     cout << msg.length << ":";     if (msg.text != nullptr) cout << msg.text;     else                     cout << "<empty>";     return outs; }

Move Semantics Example, cont’d TestMove.cpp char h[16], w[16]; strcpy(h, "hello"); strcpy(w, "world"); cout << "h = " << h << endl; cout << "w = " << w << endl; // Default constructor. cout << endl; Message msg; cout << "msg = " << msg << endl; // Regular constructor. Message hello1(h); Message world1(w); cout << "hello1 = " << hello1 << endl; cout << "world1 = " << world1 << endl; h = hello w = world *** Default constructor called. msg = 0:<empty> *** Regular constructor called: t = 'hello' *** Regular constructor called: t = 'world' hello1 = 5:hello world1 = 5:world

Move Semantics Example, cont’d // Copy constructor. cout << endl; Message hello2(hello1); cout << "hello1 = " << hello1 << endl; cout << "hello2 = " << hello2 << endl; // Default constructor and copy assignment. Message world2; world2 = world1; cout << "world1 = " << world1 << endl; cout << "world2 = " << world2 << endl; *** Copy constructor called. hello1 = 5:hello hello2 = 5:hello *** Default constructor called. *** Copy assignment called. world1 = 5:world world2 = 5:world

Move Semantics Example, cont’d // Move constructor. cout << endl; Message hello3(move(hello1)); cout << "hello1 = " << hello1 << endl; cout << "hello3 = " << hello3 << endl; // Default constructor and move assignment. Message world3; world3 = move(world1); cout << "world1 = " << world1 << endl; cout << "world3 = " << world3 << endl; // Destructor. cout << endl << "Done!" << endl; return 0; *** Move constructor called. hello1 = 0:<empty> hello3 = 5:hello *** Default constructor called. *** Move assignment called. world1 = 0:<empty> world3 = 5:world Done! *** Destructor called: text = 'world'

Review: Rule of the “Big Three” If a class defines one or more of the following, it should explicitly define all three: Destructor Copy constructor Copy assignment operator The copy constructor and the copy assignment operator each does a deep copy.

Rule of the “Big Five” Explicitly define: Destructor Copy constructor Copy assignment operator Move constructor Move assignment operator The move constructor and the move assignment operator can each steal resources from temporary source objects.

Introduction to Algorithm Analysis To analyze an algorithm, we measure it. A convenient measure must be: A resource we care about (elapsed time, memory usage, etc.). Quantitative, to make comparisons possible. Easy to compute. A good predictor of the “goodness” of the algorithm. We will be concerned mostly with elapsed time.

Introduction to Algorithm Analysis, cont’d Our concern generally is not how long a particular run of an algorithm will take, but how well the algorithm scales. How does the run time increase as the amount of input increases? Example: How does the reading time of a book increase as the number of pages increases? Example: How does the run time of a particular sort algorithm increase as the number of items to be sorted increases?

Introduction to Algorithm Analysis, cont’d When we compare two algorithms, we want to compare how well they scale. How do their elapsed run times grow as the size of the input grows? How do their growth rates compare? Can we do this comparison without actually running the algorithms? Some algorithms may be too expensive to run.

Big-Oh Notation Let T(N) be the running time of an algorithm with N input values. T(N) = O(f(N)) if there are positive constants c and n0 such that T(N) ≤ cf(N) when N ≥ n0. O is the “big-oh” pronounced “order of” In other words, when N is sufficiently large, function f(N) is an upper bound for time function T(N). We don’t care about small values of N. T(N) will grow no faster than f(N) as N increases.

Big-Oh Notation, cont’d T(N) cf(N) T(N) = O(f(N)) Upper bound

How Well Does an Algorithm Scale? Data Structures and Algorithms in Java, 3rd ed. by Mark Allen Weiss Pearson Education, Inc., 2012 ISBN 978-0-13-257627-7

How Well Does an Algorithm Scale? cont’d Data Structures and Algorithms in Java, 3rd ed. by Mark Allen Weiss Pearson Education, Inc., 2012 ISBN 978-0-13-257627-7

How Well Does an Algorithm Scale? cont’d Data Structures and Algorithms in Java, 3rd ed. by Mark Allen Weiss Pearson Education, Inc., 2012 ISBN 978-0-13-257627-7

How Well Does an Algorithm Scale? cont’d Data Structures and Algorithms in Java, 3rd ed. by Mark Allen Weiss Pearson Education, Inc., 2012 ISBN 978-0-13-257627-7

Partitioning a List of Values “Divide and conquer” sorting algorithms require that the list first be split into smaller sublists that can be sorted separately. Then the sorted sublists can be recombined into a single sorted list. The sublists are usually sorted using recursion. Are there better ways to partition (split) a list of values other than down the middle?

Partitioning a List of Values, cont’d Pick an arbitrary pivot value in the list. Move all the values less than the pivot value into one sublist. Move all the values greater than the pivot value into the other sublist. Now the pivot value is in its “final resting place”. It’s in the correct position for the sorted list. Recursively sort the two sublists. The pivot value doesn’t move. Challenge: Find a good pivot value.

Mark Allen Weiss Data Structures and Algorithms in Java (c) 2006 Pearson Education, Inc. All rights reserved. 0-13-257627-9

Partition a List Using a Pivot Given a list, pick an element to be the pivot. There are various strategies to pick the pivot. The simplest is to pick the first element of the list. First get the chosen pivot value “out of the way” by swapping with the value currently at the right end. 6 1 4 9 0 3 5 2 7 8 8 1 4 9 0 3 5 2 7 6

Partition a List Using a Pivot, cont’d 8 1 4 9 0 3 5 2 7 6 Goal: Move all values < pivot to the left part of the list and all values > pivot to the right part of the list.

Partition a List Using a Pivot, cont’d Set index i to the left end of the list and index j to one from the right end. While i < j: Move i right, skipping over values < pivot. Stop i when it reaches a value ≥ pivot. Move j left, skipping over values > pivot. Stop j when it reaches a value ≤ pivot. After both i and j have stopped, swap the values at i and j. 8 1 4 9 0 3 5 2 7 6 i j

Partition a List Using a Pivot, cont’d 8 1 4 9 0 3 5 2 7 6 i j Move j: 2 1 4 9 0 3 5 8 7 6 i j Swap: 2 1 4 9 0 3 5 8 7 6 i j Move i and j: 2 1 4 5 0 3 9 8 7 6 i j Swap: Move i and j. They’ve crossed! 2 1 4 5 0 3 9 8 7 6 j i Swap the pivot with the ith element: 2 1 4 5 0 3 6 8 7 9 j i Now the list is properly partitioned for quicksort!

Quicksort A fast divide-and-conquer sorting algorithm. Basic idea: A very tight and highly optimized inner loop. Looks like magic in animation. Average running time is O(N log N) Worst-case running time is O(N2) The worst case be made to occur very unlikely. Basic idea: Partition the list using a pivot. Recursively sort the two sublists. One of the most elegant and useful algorithms in computer science.

Quicksort Pivot Strategy Quicksort is a fragile algorithm! It is sensitive to picking a good pivot. Attempts to improve the algorithm can break it. Simplest pivot strategy: Pick the first element of each sublist. Worst strategy if the list is already sorted. Running time O(N2)

Median-of-Three Pivot Strategy A good pivot value would be the median value of the list. The median of a list of unsorted numbers is nontrivial to compute. Compromise: Examine the two values at the ends of the list and the value at the middle position of the list. Choose the value that’s in between the other two.

Optimal Quicksort Median-of-three pivoting strategy. 5 24 8 10 23 14 17 11 17 8 19 21 9 16 16 24 19 8 8 2 16 11 11 1 8 SORTING from index 0 to 24 [ 5 24 8 10 23 14 17 11 17 8 19 21 9 16 16 24 19 8 8 2 16 11 11 1 8] Paritioning with pivot 8 i = 1, j = 23, swapped 1 and 24 [ 5 1# 8 10 23 14 17 11 17 8 19 21 9 16 16 24 19 8 8 2 16 11 11 24# 8] i = 2, j = 19, swapped 2 and 8 [ 5 1 2# 10 23 14 17 11 17 8 19 21 9 16 16 24 19 8 8 8# 16 11 11 24 8] i = 3, j = 18, swapped 8 and 10 [ 5 1 2 8# 23 14 17 11 17 8 19 21 9 16 16 24 19 8 10# 8 16 11 11 24 8] i = 4, j = 17, swapped 8 and 23 [ 5 1 2 8 8# 14 17 11 17 8 19 21 9 16 16 24 19 23# 10 8 16 11 11 24 8] i = 5, j = 9, swapped 8 and 14 [ 5 1 2 8 8 8# 17 11 17 14# 19 21 9 16 16 24 19 23 10 8 16 11 11 24 8] i = 6, j = 5, swapped 17 and 8 [ 5 1 2 8 8 8# 17# 11 17 14 19 21 9 16 16 24 19 23 10 8 16 11 11 24 8] Partitioned with pivot 8 at index 6 [ 5 1 2 8 8 8 8 11 17 14 19 21 9 16 16 24 19 23 10 8 16 11 11 24 17]

Optimal Quicksort, cont’d SORTING from index 0 to 5 [ 5 1 2 8 8 8] 8 11 17 14 19 21 9 16 16 24 19 23 10 8 16 11 11 24 17 Paritioning with pivot 5 [ 2 1 8 8 8 5] 8 11 17 14 19 21 9 16 16 24 19 23 10 8 16 11 11 24 17 i = 2, j = 1, swapped 8 and 1 [ 2 1# 8# 8 8 5] 8 11 17 14 19 21 9 16 16 24 19 23 10 8 16 11 11 24 17 Partitioned with pivot 5 at index 2 [ 2 1 5 8 8 8] 8 11 17 14 19 21 9 16 16 24 19 23 10 8 16 11 11 24 17 SORTING from index 0 to 1 [ 2 1] 5 8 8 8 8 11 17 14 19 21 9 16 16 24 19 23 10 8 16 11 11 24 17 Paritioning with pivot 1 i = 0, j = -1, swapped 2 and 393240 [ 2# 1] 5 8 8 8 8 11 17 14 19 21 9 16 16 24 19 23 10 8 16 11 11 24 17 Partitioned with pivot 1 at index 0 [ 1 2] 5 8 8 8 8 11 17 14 19 21 9 16 16 24 19 23 10 8 16 11 11 24 17 SORTED from index 0 to 1

Optimal Quicksort, cont’d SORTING from index 3 to 5 1 2 5 [ 8 8 8] 8 11 17 14 19 21 9 16 16 24 19 23 10 8 16 11 11 24 17 Paritioning with pivot 8 i = 3, j = 4, swapped 8 and 8 1 2 5 [ 8# 8# 8] 8 11 17 14 19 21 9 16 16 24 19 23 10 8 16 11 11 24 17 i = 4, j = 3, swapped 8 and 8 Partitioned with pivot 8 at index 4 SORTED from index 3 to 5 SORTED from index 0 to 5 [ 1 2 5 8 8 8] 8 11 17 14 19 21 9 16 16 24 19 23 10 8 16 11 11 24 17

Optimal Quicksort, cont’d SORTING from index 7 to 24 1 2 5 8 8 8 8 [ 11 17 14 19 21 9 16 16 24 19 23 10 8 16 11 11 24 17] Paritioning with pivot 17 i = 8, j = 22, swapped 11 and 17 1 2 5 8 8 8 8 [ 11 11# 14 19 21 9 16 16 24 19 23 10 8 16 11 17# 24 17] i = 10, j = 21, swapped 11 and 19 1 2 5 8 8 8 8 [ 11 11 14 11# 21 9 16 16 24 19 23 10 8 16 19# 17 24 17] i = 11, j = 20, swapped 16 and 21 1 2 5 8 8 8 8 [ 11 11 14 11 16# 9 16 16 24 19 23 10 8 21# 19 17 24 17] i = 15, j = 19, swapped 8 and 24 1 2 5 8 8 8 8 [ 11 11 14 11 16 9 16 16 8# 19 23 10 24# 21 19 17 24 17] i = 16, j = 18, swapped 10 and 19 1 2 5 8 8 8 8 [ 11 11 14 11 16 9 16 16 8 10# 23 19# 24 21 19 17 24 17] i = 17, j = 16, swapped 23 and 10 1 2 5 8 8 8 8 [ 11 11 14 11 16 9 16 16 8 10# 23# 19 24 21 19 17 24 17] Partitioned with pivot 17 at index 17 1 2 5 8 8 8 8 [ 11 11 14 11 16 9 16 16 8 10 17 19 24 21 19 17 24 23]

Optimal Quicksort, cont’d SORTING from index 7 to 16 1 2 5 8 8 8 8 [ 11 11 14 11 16 9 16 16 8 10] 17 19 24 21 19 17 24 23 Paritioning with pivot 11 1 2 5 8 8 8 8 [ 10 11 14 11 16 9 16 16 8 11] 17 19 24 21 19 17 24 23 i = 8, j = 15, swapped 8 and 11 1 2 5 8 8 8 8 [ 10 8# 14 11 16 9 16 16 11# 11] 17 19 24 21 19 17 24 23 i = 9, j = 12, swapped 9 and 14 1 2 5 8 8 8 8 [ 10 8 9# 11 16 14# 16 16 11 11] 17 19 24 21 19 17 24 23 i = 10, j = 10, swapped 11 and 11 1 2 5 8 8 8 8 [ 10 8 9 11# 16 14 16 16 11 11] 17 19 24 21 19 17 24 23 Partitioned with pivot 11 at index 10 1 2 5 8 8 8 8 [ 10 8 9 11 16 14 16 16 11 11] 17 19 24 21 19 17 24 23 SORTING from index 7 to 9 1 2 5 8 8 8 8 [ 10 8 9] 11 16 14 16 16 11 11 17 19 24 21 19 17 24 23 Paritioning with pivot 9 1 2 5 8 8 8 8 [ 8 10 9] 11 16 14 16 16 11 11 17 19 24 21 19 17 24 23 i = 8, j = 7, swapped 10 and 8 1 2 5 8 8 8 8 [ 8# 10# 9] 11 16 14 16 16 11 11 17 19 24 21 19 17 24 23 Partitioned with pivot 9 at index 8 1 2 5 8 8 8 8 [ 8 9 10] 11 16 14 16 16 11 11 17 19 24 21 19 17 24 23 SORTED from index 7 to 9

Optimal Quicksort, cont’d SORTING from index 11 to 16 1 2 5 8 8 8 8 8 9 10 11 [ 16 14 16 16 11 11] 17 19 24 21 19 17 24 23 Paritioning with pivot 16 1 2 5 8 8 8 8 8 9 10 11 [ 11 14 16 16 11 16] 17 19 24 21 19 17 24 23 i = 13, j = 15, swapped 11 and 16 1 2 5 8 8 8 8 8 9 10 11 [ 11 14 11# 16 16# 16] 17 19 24 21 19 17 24 23 i = 14, j = 14, swapped 16 and 16 1 2 5 8 8 8 8 8 9 10 11 [ 11 14 11 16# 16 16] 17 19 24 21 19 17 24 23 Partitioned with pivot 16 at index 14 1 2 5 8 8 8 8 8 9 10 11 [ 11 14 11 16 16 16] 17 19 24 21 19 17 24 23 SORTING from index 11 to 13 1 2 5 8 8 8 8 8 9 10 11 [ 11 14 11] 16 16 16 17 19 24 21 19 17 24 23 Paritioning with pivot 11 i = 11, j = 11, swapped 11 and 11 1 2 5 8 8 8 8 8 9 10 11 [ 11# 14 11] 16 16 16 17 19 24 21 19 17 24 23 Partitioned with pivot 11 at index 11

Optimal Quicksort, cont’d SORTING from index 12 to 13 1 2 5 8 8 8 8 8 9 10 11 11 [ 14 11] 16 16 16 17 19 24 21 19 17 24 23 Paritioning with pivot 11 i = 12, j = 11, swapped 14 and 11 1 2 5 8 8 8 8 8 9 10 11 11# [ 14# 11] 16 16 16 17 19 24 21 19 17 24 23 Partitioned with pivot 11 at index 12 1 2 5 8 8 8 8 8 9 10 11 11 [ 11 14] 16 16 16 17 19 24 21 19 17 24 23 SORTED from index 12 to 13 SORTED from index 11 to 13 1 2 5 8 8 8 8 8 9 10 11 [ 11 11 14] 16 16 16 17 19 24 21 19 17 24 23 SORTING from index 15 to 16 1 2 5 8 8 8 8 8 9 10 11 11 11 14 16 [ 16 16] 17 19 24 21 19 17 24 23 Paritioning with pivot 16 i = 15, j = 15, swapped 16 and 16 1 2 5 8 8 8 8 8 9 10 11 11 11 14 16 [ 16# 16] 17 19 24 21 19 17 24 23 Partitioned with pivot 16 at index 15 SORTED from index 15 to 16 SORTED from index 11 to 16 1 2 5 8 8 8 8 8 9 10 11 [ 11 11 14 16 16 16] 17 19 24 21 19 17 24 23 SORTED from index 7 to 16 1 2 5 8 8 8 8 [ 8 9 10 11 11 11 14 16 16 16] 17 19 24 21 19 17 24 23

Optimal Quicksort, cont’d SORTING from index 18 to 24 1 2 5 8 8 8 8 8 9 10 11 11 11 14 16 16 16 17 [ 19 24 21 19 17 24 23] Paritioning with pivot 19 1 2 5 8 8 8 8 8 9 10 11 11 11 14 16 16 16 17 [ 19 24 21 23 17 24 19] i = 18, j = 22, swapped 17 and 19 1 2 5 8 8 8 8 8 9 10 11 11 11 14 16 16 16 17 [ 17# 24 21 23 19# 24 19] i = 19, j = 18, swapped 24 and 17 1 2 5 8 8 8 8 8 9 10 11 11 11 14 16 16 16 17 [ 17# 24# 21 23 19 24 19] Partitioned with pivot 19 at index 19 1 2 5 8 8 8 8 8 9 10 11 11 11 14 16 16 16 17 [ 17 19 21 23 19 24 24] SORTING from index 20 to 24 1 2 5 8 8 8 8 8 9 10 11 11 11 14 16 16 16 17 17 19 [ 21 23 19 24 24] Paritioning with pivot 21 1 2 5 8 8 8 8 8 9 10 11 11 11 14 16 16 16 17 17 19 [ 19 23 24 24 21] i = 21, j = 20, swapped 23 and 19 1 2 5 8 8 8 8 8 9 10 11 11 11 14 16 16 16 17 17 19 [ 19# 23# 24 24 21] Partitioned with pivot 21 at index 21 1 2 5 8 8 8 8 8 9 10 11 11 11 14 16 16 16 17 17 19 [ 19 21 24 24 23]

Optimal Quicksort, cont’d SORTING from index 22 to 24 1 2 5 8 8 8 8 8 9 10 11 11 11 14 16 16 16 17 17 19 19 21 [ 24 24 23] Paritioning with pivot 24 1 2 5 8 8 8 8 8 9 10 11 11 11 14 16 16 16 17 17 19 19 21 [ 23 24 24] i = 23, j = 23, swapped 24 and 24 1 2 5 8 8 8 8 8 9 10 11 11 11 14 16 16 16 17 17 19 19 21 [ 23 24# 24] Partitioned with pivot 24 at index 23 SORTED from index 22 to 24 SORTED from index 20 to 24 1 2 5 8 8 8 8 8 9 10 11 11 11 14 16 16 16 17 17 19 [ 19 21 23 24 24] SORTED from index 18 to 24 1 2 5 8 8 8 8 8 9 10 11 11 11 14 16 16 16 17 [ 17 19 19 21 23 24 24] SORTED from index 7 to 24 1 2 5 8 8 8 8 [ 8 9 10 11 11 11 14 16 16 16 17 17 19 19 21 23 24 24] SORTED from index 0 to 24 [ 1 2 5 8 8 8 8 8 9 10 11 11 11 14 16 16 16 17 17 19 19 21 23 24 24]

Quicksort, cont’d Quicksort doesn’t do well for very short lists. When a sublist becomes too small, use another algorithm to sort the sublist such as insertion sort.

Sorting Animations https://www.cs.usfca.edu/~galles/visualization/ComparisonSort.html http://www.sorting-algorithms.com