Real-world sorting (not on exam)

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

Better sorting algorithms (Weiss chapter )

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

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

Quicksort (Weiss chapter 8.6)

CSC 273 Data Structures

CS240 Fall Mike Lam, Professor. Quick Sort

Faster Sorting Methods

Divide and Conquer Sorting Algorithms and Noncomparison-based

Sorting. Weiss chapter , 8.6

CS 171: Introduction to Computer Science II. Quicksort

CS 171: Introduction to Computer Science II. Quicksort

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

COT 5407: Introduction to Algorithms. Giri Narasimhan. ECS 254A; Phone: x3748

CS61BL. Lecture 5: Graphs Sorting

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

L14 Quicksort and Performance Optimization

Algorithmic Analysis and Sorting, Part Two

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

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

Sorting. Dr. Baldassano Yu s Elite Education

Sorting (I) Hwansoo Han

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

COSC242 Lecture 7 Mergesort and Quicksort

Algorithmic Analysis and Sorting, Part Two. CS106B Winter

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

QuickSort

CSE 373 MAY 24 TH ANALYSIS AND NON- COMPARISON SORTING

CS1 Lecture 30 Apr. 2, 2018

II (Sorting and) Order Statistics

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

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

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

Sorting Algorithms. + Analysis of the Sorting Algorithms

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)

Topic 17 Fast Sorting

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

CS61B, Spring 2003 Discussion #15 Amir Kamil UC Berkeley 4/28/03

About this exam review

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

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

Programming Challenges - Week 3

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

DIVIDE AND CONQUER ALGORITHMS ANALYSIS WITH RECURRENCE EQUATIONS

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

Programming II (CS300)

CS61B Lectures # Purposes of Sorting. Some Definitions. Classifications. Sorting supports searching Binary search standard example

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

CHAPTER 7 Iris Hui-Ru Jiang Fall 2008

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

Quick Sort. CSE Data Structures May 15, 2002

CSE 373: Data Structures and Algorithms

"Organizing is what you do before you do something, so that when you do it, it is not all mixed up." ~ A. A. Milne SORTING

COMP Data Structures

COMP26120 Academic Session: Lab Exercise 6: Recursive Sorting and Searching

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

Question 7.11 Show how heapsort processes the input:

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

Sorting Goodrich, Tamassia Sorting 1

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

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

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

Searching in General

ITEC2620 Introduction to Data Structures

QUICKSORT TABLE OF CONTENTS

Divide and Conquer Algorithms: Advanced Sorting

DATA STRUCTURES AND ALGORITHMS

Sorting and Selection

CS 206 Introduction to Computer Science II

Introduction to Programming: Lecture 6

First Swedish Workshop on Multi-Core Computing MCC 2008 Ronneby: On Sorting and Load Balancing on Graphics Processors

Unit 6 Chapter 15 EXAMPLES OF COMPLEXITY CALCULATION

CSE373 Fall 2013, Final Examination December 10, 2013 Please do not turn the page until the bell rings.

CSE373 Fall 2013, Final Examination December 10, 2013 Please do not turn the page until the bell rings.

Data Structures and Algorithms

Topics for CSCI 151 Final Exam Wednesday, May 10

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

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

Design and Analysis of Algorithms

Data Structures and Algorithms 2018

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

CSCI 204 Introduction to Computer Science II

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

UNIT 5C Merge Sort. Course Announcements

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

Sorting (Weiss chapter )

COT 5407: Introduction. to Algorithms. Giri NARASIMHAN. 1/29/19 CAP 5510 / CGS 5166

Chapter 7 Sorting. Terminology. Selection Sort

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

CSE 373 Lecture 19: Wrap-Up of Sorting

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

Lecture 8: Mergesort / Quicksort Steven Skiena

CSE 326: Data Structures Sorting Conclusion

CS102 Sorting - Part 2

CS 311 Data Structures and Algorithms, Spring 2009 Midterm Exam Solutions. The Midterm Exam was given in class on Wednesday, March 18, 2009.

Data Structures Brett Bernstein

Assignment 4: Question 1b omitted. Assignment 5: Question 1b clarification

Overview of Sorting Algorithms

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

Shell s sort. CS61B Lecture #25. Sorting by Selection: Heapsort. Example of Shell s Sort

Transcription:

Real-world sorting (not on exam)

Sorting algorithms so far Insertion sort Worst case Average case Best case O(n 2 ) O(n 2 ) O(n) Quicksort O(n 2 ) O(n log n) O(n log n) Mergesort O(n log n) O(n log n) O(n log n)

Sorting algorithms so far Insertion sort Worst case Average case Best case O(n 2 ) O(n2) O(n) Quicksort O(n 2 ) No clear winner... O(n log n) the best algorithms O(n log n) Mergesort O(n log n) combine O(n ideas log n) from several O(n log n)

Introsort Quicksort: fast in practice, but O(n 2 ) worst case Introsort: Start with Quicksort If the recursion depth gets too big, switch to heapsort, which is O(n log n) Plus standard Quicksort optimisations: Choose pivot via median-of-three Switch to insertion sort for small arrays Used by e.g. C++ STL,.NET,...

Dual-pivot quicksort Instead of one pivot, pick two Instead of partitioning the array into two pieces, partition it into three pieces If pivots are x and y, then: elements < x elements > x and < y elements > y Same complexity as Quicksort, but fewer recursive calls because the array gets up quicker Used by Java for primitive types (int,...)

Traditional sort (a recap)

1 3 6 8 4 2 1 3 7 1 3 6 8 4 2 1 3 7 1 3 6 8 4 2 1 3 7 1 3 2 1 1 3 6 8 4 2 1 3 7

1 3 6 8 4 2 1 3 7 1 3 1 2 1 3 6 4 8 1 2 3 7 1 3 4 6 8 1 2 3 7 1 1 2 3 3 4 6 7 8

Natural sort Traditional sort s the input list into single elements before merging everything back together again Better idea: the input into runs A run is a sequence of increasing elements...or a sequence of decreasing elements First reverse all the decreasing runs, so they become increasing Then all the runs together

1 3 6 8 4 2 1 3 7 1 3 6 8 4 2 1 3 7 reverse 1 2 4 1 1 2 3 4 6 8 3 7 1 1 2 3 3 4 6 7 8

Natural sort Big advantage: O(n) time for sorted data...and reverse-sorted data...and almost -sorted data Complexity: O(n log r), where r is the number of runs in the input...worst case, each run has two elements, so r = n/2, so O(n log n) Used by GHC

1 3 6 8 4 2 1 3 7 1 3 6 8 4 2 1 3 7 reverse Total time is O(n 1log 2 r)! 4 O (log r) levels 1 1 2 3 4 6 8 3 7 1 1 2 3 3 4 6 7 8 O(n) time per level

Timsort Natural sort is really good on sorted/nearly-sorted data You get long runs so not many s to do But not so good on random data Short runs so many s Idea of Timsort: on short random parts of the list, switch to insertion sort How to detect randomness? Several short runs next to one another

1 3 6 8 4 2 1 3 7 Don't 3 7 into two runs, it's too short! reverse 1 3 6 8 4 2 1 3 7 1 2 4 insertion sort 1 1 2 3 4 6 8 3 7 1 1 2 3 3 4 6 7 8

Timsort Specifically: If we come across a short run, join it together with all following short runs until we reach a threshold Then use insertion sort on that part Also some optimisations for : Merge smaller runs together first If the begins with several elements from the same array, use binary search to find out how many and then copy them all in one go Used in Java for arrays of objects, Python http://en.wikipedia.org/wiki/timsort

The best sorting algorithm? A good sorting algorithm should: have O(n log n) complexity have O(n) complexity on nearly-sorted data be simple be in-place be stable cache-friendly No algorithm seems to have all of these!

Summary No one-size fits all answer Best overall complexity: natural sort But quicksort has smaller constant factors Different algorithms are good in different situations...you should find out which in the lab :) Best sorting functions combine ideas from several algorithms Introsort: quicksort+heapsort+insertion sort Timsort: natural sort+insertion sort

Special-purpose sorting algorithms (not on exam)

Sorting integers All the algorithms we've seen work by comparing elements of the list with Advantage: general purpose If the list contains integers, we can do something different! Counting sort: http://en.wikipedia.org/wiki/counting_sort Bucket sort: http://en.wikipedia.org/wiki/bucket_sort Radix sort (dates from 1887!): http://en.wikipedia.org/wiki/radix_sort