Data structures. More sorting. Dr. Alex Gerdes DIT961 - VT 2018

Similar documents
Better sorting algorithms (Weiss chapter )

Quicksort (Weiss chapter 8.6)

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

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

Sorting. Weiss chapter , 8.6

Real-world sorting (not on exam)

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

Algorithmic Analysis and Sorting, Part Two

Divide and Conquer Sorting Algorithms and Noncomparison-based

COSC242 Lecture 7 Mergesort and Quicksort

CS 171: Introduction to Computer Science II. Quicksort

Cosc 241 Programming and Problem Solving Lecture 17 (30/4/18) Quicksort

Lecture 8: Mergesort / Quicksort Steven Skiena

CS 310 Advanced Data Structures and Algorithms

Sorting Algorithms. + Analysis of the Sorting Algorithms

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

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

Algorithmic Analysis and Sorting, Part Two. CS106B Winter

CS 206 Introduction to Computer Science II

We can use a max-heap to sort data.

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

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

Faster Sorting Methods

BM267 - Introduction to Data Structures

ITEC2620 Introduction to Data Structures

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

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

Sorting Algorithms Day 2 4/5/17

Overview of Sorting Algorithms

DATA STRUCTURES AND ALGORITHMS

Data Structures and Algorithms

Lecture 7 Quicksort : Principles of Imperative Computation (Spring 2018) Frank Pfenning

Sorting. CSE 143 Java. Insert for a Sorted List. Insertion Sort. Insertion Sort As A Card Game Operation. CSE143 Au

Run Times. Efficiency Issues. Run Times cont d. More on O( ) notation

Sorting. CSC 143 Java. Insert for a Sorted List. Insertion Sort. Insertion Sort As A Card Game Operation CSC Picture

Unit-2 Divide and conquer 2016

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

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

Sorting Goodrich, Tamassia Sorting 1

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

DIVIDE AND CONQUER ALGORITHMS ANALYSIS WITH RECURRENCE EQUATIONS

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

Data Structures and Algorithms

Divide and Conquer Algorithms: Advanced Sorting

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

CS240 Fall Mike Lam, Professor. Quick Sort

Divide and Conquer Algorithms: Advanced Sorting. Prichard Ch. 10.2: Advanced Sorting Algorithms

Sorting. Data structures and Algorithms

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

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

Lecture Notes on Quicksort

CSE 373: Data Structures and Algorithms

Unit Outline. Comparing Sorting Algorithms Heapsort Mergesort Quicksort More Comparisons Complexity of Sorting 2 / 33

Algorithmic Analysis. Go go Big O(h)!

CSE 373 MAY 24 TH ANALYSIS AND NON- COMPARISON SORTING

SORTING, SETS, AND SELECTION

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

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

Unit 6 Chapter 15 EXAMPLES OF COMPLEXITY CALCULATION

CIS 121 Data Structures and Algorithms with Java Spring Code Snippets and Recurrences Monday, January 29/Tuesday, January 30

CS61BL. Lecture 5: Graphs Sorting

Quick-Sort. Quick-Sort 1

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

// 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];

Quicksort. The divide-and-conquer strategy is used in quicksort. Below the recursion step is described:

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

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

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

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

Quick Sort. CSE Data Structures May 15, 2002

Question 7.11 Show how heapsort processes the input:

Sorting. Dr. Baldassano Yu s Elite Education

CSE 373 NOVEMBER 8 TH COMPARISON SORTS

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

CSC 273 Data Structures

CS 171: Introduction to Computer Science II. Quicksort

Algorithms in Systems Engineering ISE 172. Lecture 12. Dr. Ted Ralphs

CSE 143. Two important problems. Searching and Sorting. Review: Linear Search. Review: Binary Search. Example. How Efficient Is Linear Search?

Presentation for use with the textbook, Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, 2015

Programming II (CS300)

CPSC 311 Lecture Notes. Sorting and Order Statistics (Chapters 6-9)

CS1 Lecture 30 Apr. 2, 2018

Comparison Sorts. Chapter 9.4, 12.1, 12.2

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.

Quick-Sort fi fi fi 7 9. Quick-Sort Goodrich, Tamassia

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

Arrays, Vectors Searching, Sorting

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

Divide and Conquer 4-0

Chapter 7 Sorting. Terminology. Selection Sort

CS102 Sorting - Part 2

Module 3: Sorting and Randomized Algorithms

Sorting (I) Hwansoo Han

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

Randomized Algorithms, Quicksort and Randomized Selection

Divide and Conquer. Algorithm D-and-C(n: input size)

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

QUICKSORT TABLE OF CONTENTS

CS 206 Introduction to Computer Science II

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

Transcription:

Data structures More sorting Dr. Alex Gerdes DIT961 - VT 2018

Divide and conquer Very general name for a type of recursive algorithm You have a problem to solve: - Split that problem into smaller subproblems - Recursively solve those subproblems - Combine the solutions for the subproblems to solve the whole problem

To solve this

Divide and conquer 1. Split the problem into subproblems 2. Recursively solve the subproblems 3. Combine the solutions

Quicksort Pick an element from the array, called the pivot Partition the array: - First come all the elements smaller than the pivot, then the pivot, then all the elements greater than the pivot Recursively quicksort the two partitions

Quicksort 5 3 9 2 8 7 3 2 1 4 Say the pivot is 5. Partition the array into: all elements less than 5, then 5, then all elements greater than 5 3 3 2 2 1 4 5 9 8 7 Less than the pivot Greater than the pivot

Quicksort Now recursively quick sort the two partitions! 3 3 2 2 1 4 5 9 8 7 Quicksort Quicksort 1 2 2 3 3 4 5 7 8 9 Less than the pivot Greater than the pivot

Pseudocode // call as sort(a, 0, a.length-1); void sort(int[] a, int low, int high) { if (low >= high) return; int pivot = partition(a, low, high); // assume that partition returns the // index where the pivot now is sort(a, low, pivot-1); sort(a, pivot+1, high); } Common optimisation: switch to insertion sort when the input array is small

Quiz What is the complexity of quicksort? (assuming partition is O(n)) O(log n) O(n) O(n log n) O(n 2 ) Vet ej

Complexity of quick sort In the best case, partitioning splits an array of size n into two halves of size n/2: n n/2 n/2

Complexity of quick sort The recursive calls will split these arrays into four arrays of size n/4: intofoura raysofsizen/4: n n/2 n/2 n/4 n/4 n/4 n/4

Complexity of quick sort n n/2 Total time is n/2 O(n log n)! n/4 n/4 n/4 n/4 log n levels n/8 n/8 n/8 n/8 n/8 n/8 n/8 n/8 O(n) time per level

Complexity of quicksort But that's the best case! In the worst case, everything is greater than the pivot (say) - The recursive call has size n-1 - Which in turn recurses with size n-2, etc. - Amount of time spent in partitioning: n + (n-1) + (n-2) +... + 1 = O(n 2 )

n n-1 Total time is O(n 2 )! n-2 n levels n-3 O(n) time per level

Worst cases When we pick the first element as the pivot, we get this worst case for: - Sorted arrays - Reverse-sorted arrays The best pivot to use is the median value of the array, but in practice it's too expensive to compute... Most important decision in QuickSort: what to use as the pivot You don't need to split the array into exactly equal parts, it's enough to have some balance (e.g. 10%/90% split still gives O(n log n) runtime)

Complexity of quicksort Quicksort works well when the pivot splits the array into roughly equal parts - Median-of-three: pick first, middle and last element of the array and pick the median of those three - Pick pivot at random: gives O(n log n) expected (probabilistic) complexity Introsort: detect when we get into the O(n 2 ) case and switch to a different algorithm (e.g. heapsort)

Partitioning algorithm 1. Pick a pivot (here 5) 5 3 9 2 8 7 3 2 1 4

Partitioning algorithm 2. Set two indexes, low and high 5 3 9 2 8 7 3 2 1 4 low high Idea: everything to the left of low is less than the pivot (coloured yellow), everything to the right of high is greater than the pivot (orange)

Partitioning algorithm 3. Move low right until you find something greater than the pivot 5 3 9 2 8 7 3 2 1 4 low high while (a[low] < pivot) low++;

Partitioning algorithm 3. Move low right until you find something greater than the pivot 5 3 9 2 8 7 3 2 1 4 low high while (a[low] < pivot) low++;

Partitioning algorithm 3. Move low right until you find something greater than the pivot 5 3 9 2 8 7 3 2 1 4 low high while (a[low] < pivot) low++;

Partitioning algorithm 3. Move low right until you find something greater than the pivot 5 3 9 2 8 7 3 2 1 4 low high while (a[high] < pivot) high ;

Partitioning algorithm 4. Swap them! 5 3 4 2 8 7 3 2 1 9 low high swap(a, low, high);

Partitioning algorithm 5. Advance low and high and repeat 5 3 4 2 8 7 3 2 1 9 low high low++; high ;

Partitioning algorithm Move low until higher than pivot 5 3 4 2 8 7 3 2 1 9 low high

Partitioning algorithm Move high until lower than pivot 5 3 4 2 8 7 3 2 1 9 low high

Partitioning algorithm Swap low and high 5 3 4 2 1 7 3 2 8 9 low high

Partitioning algorithm Advance and repeat 5 3 4 2 1 7 3 2 8 9 low high

Partitioning algorithm Move low and then high 5 3 4 2 1 7 3 2 8 9 low high

Partitioning algorithm Swap and advance 5 3 4 2 1 2 3 7 8 9 low high

Partitioning algorithm Move high and low 5 3 4 2 1 2 3 7 8 9 low high

Partitioning algorithm 6. When low and high have crossed, we are finished! 5 3 4 2 1 2 3 7 8 9 low high But the pivot is in the wrong place

Partitioning algorithm 7. Final step: swap pivot with high 3 3 4 2 1 2 5 7 8 9 low high But the pivot is in the wrong place

KWICK SÖRT idea-instructions.com/quick-sort/ v1.0, CC by-nc-sa 4.0 4 x 1x 1x 5 1x 1 2 6 KWICK SÖRT KWICK SÖRT 3

Details 1. What to do if the pivot is not the first element? - Swap the pivot with the first element before starting partitioning! 2. What happens if the array contains many duplicates? - Notice that we only advance a[low] as long as a[low] < pivot - If a[low] == pivot we stop, same for a[high] - If the array contains just one element over and over again, low and high will advance at the same rate - Hence we get equal-sized partitions

Pivot Which pivot should we pick? - First element: gives O(n 2 ) behaviour for already- sorted lists - Median-of-three: pick first, middle and last element of the array and pick the median of those three - Pick pivot at random: gives O(n log n) expected (probabilistic) complexity

Quicksort Typically the fastest sorting algorithm......but very sensitive to details! - Must choose a good pivot to avoid O(n 2 ) case - Must take care with duplicates - Switch to insertion sort for small arrays to get better constant factors If you do all that right, you get an in-place sorting algorithm, with low constant factors and O(n log n) complexity

Mergesort

Mergesort We can merge two sorted lists into one in linear time: 2 3 5 8 9 1 2 3 4 7 1 2 2 3 3 4 5 7 8 9

Mergesort Another divide-and-conquer algorithm To mergesort a list: - Split the list into two equal parts - Recursively mergesort the two parts - Merge the two sorted lists together

Mergesort 1. Split the list into two equal parts 5 3 9 2 8 7 3 2 1 4 5 3 9 2 8 7 3 2 1 4

Mergesort 2. Recursively mergesort the two parts 5 3 9 2 8 7 3 2 1 4 2 3 5 8 9 1 2 3 4 7

Mergesort 3. Merge the two sorted lists together 2 3 5 8 9 1 2 3 4 7 1 2 2 3 3 4 5 7 8 9

Complexity analysis Mergesort's divide-and-conquer approach is similar to quicksort But it always splits the list into equally- sized pieces! Hence O(n log n), just like the best case for quicksort but this is the worst case for mergesort

Complexity of quick sort n n/2 Total time is n/2 O(n log n)! n/4 n/4 n/4 n/4 log n levels n/8 n/8 n/8 n/8 n/8 n/8 n/8 n/8 O(n) time per level

Mergesort vs quicksort Mergesort: - Not in-place - O(n log n) - Only requires sequential access to the list this makes it good in functional programming Quicksort: - In-place - O(n log n) but O(n 2 ) if you are not careful - Works on arrays only (random access) - Unstable Both the best in their fields! - Quicksort best imperative algorithm - Mergesort best functional algorithm

Stable sorting When sorting complex objects, e.g. where each element contains various information about a person, the ordering may only take part of the data in account (via Comparable, Comparator, Ord) Then it s sometimes important that objects that are deemed equal by the ordering should appear in the same order as they did in the original list A sorting algorithm that does not change the order of equal elements is called stable

Stable sorting Let s say that we want to sort [(5, a ), (3, d ), (2, f ), (3, b )] and that the ordering of the pairs is defined to be the natural ordering of the first component Unstable sorting might result in [(2, f ), (3, b ), (3, d ), (5, a )] Stable sorting always gives [(2, f ), (3, d ), (3, b ), (5, a )] Insertion sort is stable (provided that the insert inequality check is the right one, so that equal elements are not swapped).