Sorting. Dr. Baldassano Yu s Elite Education

Similar documents
CSE 373 NOVEMBER 8 TH COMPARISON SORTS

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

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

Divide and Conquer Sorting Algorithms and Noncomparison-based

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

Sorting and Selection

Real-world sorting (not on exam)

Sorting Algorithms. + Analysis of the Sorting Algorithms

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

Searching, Sorting. part 1

DIVIDE AND CONQUER ALGORITHMS ANALYSIS WITH RECURRENCE EQUATIONS

Algorithms and Applications

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

Chapter 7 Sorting. Terminology. Selection Sort

About this exam review

CS 171: Introduction to Computer Science II. Quicksort

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

Algorithmic Analysis and Sorting, Part Two

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

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

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

Outline Purpose How to analyze algorithms Examples. Algorithm Analysis. Seth Long. January 15, 2010

Question And Answer.

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

Data Structures. Sorting. Haim Kaplan & Uri Zwick December 2013

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

CS : Data Structures

S1) It's another form of peak finder problem that we discussed in class, We exploit the idea used in binary search.

Unit 6 Chapter 15 EXAMPLES OF COMPLEXITY CALCULATION

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

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

CS240 Fall Mike Lam, Professor. Quick Sort

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

Comparison Sorts. Chapter 9.4, 12.1, 12.2

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

CS61BL. Lecture 5: Graphs Sorting

CSE 373 MAY 24 TH ANALYSIS AND NON- COMPARISON SORTING

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

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

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

Data Structures and Algorithms

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

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

CSE 373: Data Structures and Algorithms

The Limits of Sorting Divide-and-Conquer Comparison Sorts II

CmpSci 187: Programming with Data Structures Spring 2015

Parallel Sorting Algorithms

Question 7.11 Show how heapsort processes the input:

Programming Challenges - Week 3

Algorithmic Analysis and Sorting, Part Two. CS106B Winter

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

ECE 242 Data Structures and Algorithms. Advanced Sorting I. Lecture 16. Prof.

1 5,9,2,7,6,10,4,3,8,1 The first number (5) is automatically the first number of the sorted list

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

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

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

Quick Sort. CSE Data Structures May 15, 2002

CSE 373: Data Structures and Algorithms

Sorting (I) Hwansoo Han

Lab 9: More Sorting Algorithms 12:00 PM, Mar 21, 2018

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

COMP 352 FALL Tutorial 10

Dual Sorting Algorithm Based on Quick Sort

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)

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

Arrays, Vectors Searching, Sorting

Lecture 6. Binary Search Trees and Red-Black Trees

CS 171: Introduction to Computer Science II. Quicksort

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

Intro to Algorithms. Professor Kevin Gold

L14 Quicksort and Performance Optimization

Quicksort (Weiss chapter 8.6)

Better sorting algorithms (Weiss chapter )

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

Cpt S 122 Data Structures. Sorting

Sorting Algorithms Day 2 4/5/17

COSC242 Lecture 7 Mergesort and Quicksort

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

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

COMP 250 Fall recurrences 2 Oct. 13, 2017

ITEC2620 Introduction to Data Structures

SORTING, SETS, AND SELECTION

Lecture 3: Sorting 1

How to Win Coding Competitions: Secrets of Champions. Week 3: Sorting and Search Algorithms Lecture 7: Lower bound. Stable sorting.

Sorting Goodrich, Tamassia Sorting 1

Design and Analysis of Algorithms

COMP Data Structures

CSCI 204 Introduction to Computer Science II

SORTING AND SELECTION

COSC 311: ALGORITHMS HW1: SORTING

Algorithm Efficiency & Sorting. Algorithm efficiency Big-O notation Searching algorithms Sorting algorithms

// 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 (Weiss chapter )

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

Computational Complexity Analysis

CSE 373: Data Structures and Algorithms

Jana Kosecka. Linear Time Sorting, Median, Order Statistics. Many slides here are based on E. Demaine, D. Luebke slides

Sorting Algorithms Spring 2019 Mentoring 10: 18 April, Asymptotics Potpourri

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

Transcription:

Sorting Dr. Baldassano Yu s Elite Education

Last week recap Algorithm: procedure for computing something Data structure: system for keeping track for information optimized for certain actions Good algorithms have time and memory requirements that scale slowly with data size Big O notation gives scaling behavior for most expensive part of algorithm O(log n) or O(n) great! O(n 2 ) may be okay O(2 n ) or O(n!) very bad!

Sorting Sorting: taking a bunch of objects and putting them in order Why do we care? An important piece of many other algorithms A good example of lots of algorithms concepts We can prove that we ve found the best possible sorting algorithms (in big O sense)

Last week s assignment

Stable sorting Sometimes the numbers we re sorting are attached to a more complicated piece of data, so identical numbers correspond to different things Often want a sort to be stable: want identical numbers to remain in the same order after sorting

Stable sorting Sort first by number, then by suit Don t want second sort to mess up first one

Comparison Table Name Avg. Time Memory Stable? Bubble O(n 2 ) O(1) Yes

Things could be worse: Bogosort The stupidest possible sorting algorithm: randomly shuffle the items, then check to see if it is sorted There are O(n!) shuffles and each check takes O(n), so this has running time O(n*n!) Not stable At least it doesn t require any extra memory!

Comparison Table Name Avg. Time Memory Stable? Bubble O(n 2 ) O(1) Yes Bogo O(n*n!) O(1) No

Insertion sort The most intuitive sorting algorithm One at a time, insert items into a sorted list on the left side

Insertion sort The most intuitive sorting algorithm One at a time, insert items into a sorted list on the left side

Big O of insertion sort Have to insert O(n) elements Will have to move O(n) elements on each insertion Avg running time O(n 2 ), and stable In practice, insertion sort tends to be better than bubble sort Sometimes the very fastest sort for short lists (<10 elements) Variant called selection sort (more compares, fewer shifts)

Comparison Table Name Avg. Time Memory Stable? Bubble O(n 2 ) O(1) Yes Bogo O(n*n!) O(1) No Insertion O(n 2 ) O(1) Yes

Mergesort It s easy to merge together two sorted lists: 1 4 7 10 3 5 6 11 To sort a list, first sort the left side, then sort the right side, then merge the two lists together This is a recursive sort, since mergesort will call itself on each half of the list

Mergesort

Big O for Mergesort There log(n) splitting levels Each element will have to be merged at each level Avg running time O(n * log(n)) BUT requires extra O(n) memory Stable sort

Comparison Table Name Avg. Time Memory Stable? Bubble O(n 2 ) O(1) Yes Bogo O(n*n!) O(1) No Insertion O(n 2 ) O(1) Yes Merge O(n*log(n)) O(n) Yes

Quicksort Somewhat complicated, but probably the most common sorting algorithm used in practice Also recursive, but with opposite logic from mergesort: To sort an array, first get the smaller items on the left and the larger items on the right, then sort the left and right arrays Pick a pivot item to define small vs. large

Quicksort

Big O for Quicksort On average takes O(log n) splits, and each level of splitting looks at all O(n) items Avg running time O(n*log(n)) Only requires O(log(n)) extra memory, to keep track of the recursive splits BUT not stable Can be made stable, but requires some extra complexity and O(n) extra space Also, has O(n 2 ) worst-case running time (if pivots are very unbalanced)

Comparison Table Name Avg. Time Memory Stable? Bubble O(n 2 ) O(1) Yes Bogo O(n*n!) O(1) No Insertion O(n 2 ) O(1) Yes Merge O(n*log(n)) O(n) Yes Quick O(n*log(n)) O(log(n)) Depends

Real running time 100 1,000 10,000 100,000 1,000,000 Bubble 0.050 5.93 445.92 44677.46 - Insertion 0.015 1.72 126.41 12478.55 - Merge 0.016 0.22 2.44 29.38 340.39 Quick 0.011 0.16 1.67 20.01 236.51 From: http://ddeville.me/2010/10/sorting-algorithms-comparison/

Visualizations https://www.cs.usfca.edu/~galles/visualization/compa risonsort.html http://bost.ocks.org/mike/algorithms/#sorting

Can we do better? Our best sorts are running in O(n log n) Is it possible to run faster? What is the minimum number of decisions a sorting algorithm needs to make?

Optimal sorting There are a total of n! possible ways to order a list we need to pick one of these orders Every time we compare two numbers x,y in a sorting algorithm, we get one of two answers: x should go first, or y should go first D decisions -> 2 D possible outcomes

Optimal sorting We need 2 D = n! Use Stirling s approximation: Taking log of both sides, D = O(n log n) So any sort that works by making comparisons must have average running time at least O(n log n)

Doing the impossible Do a first pass, counting how many of each number there are Can then calculate where each number should go Input: 5 1 3 6 3 6 Number 1 2 3 4 5 6 Count 1 0 2 0 1 2 Cumulative 1 1 3 3 4 6 Position 1-2,3-4 5,6 Output: 1 3 3 5 6 6

Counting sort This is called counting sort This is a stable sort that runs in linear O(n) time!! How did we beat the theoretical bound? This is not a comparison sort we never compare the items to one another

Non-comparison sorts Rather than comparing items, we directly calculate an item s position in the output list Catches: The keys we re sorting need to come from a limited set Requires O(n) extra space to store counting table and output array Comparison sorts are more general, requiring only some way to compare the items

Radix sort How to sort integers with 8 digits? Could use counting sort, with a huge table Let s use counting sort on each digit, repeating where necessary: 43028585 11474012 11474012 32820239 32820239 32820239 11474012 38572023 38572023 38572023 43028585 42581562 42581562 42581562 43028585

Radix sort Left-right: Most Significant Digit radix sort Have to keep track of create groups to sort within, and isn t stable Usually use Least Significant Digit radix sort, moving right to left 412 751 412 412 482 412 751 482 994 482 482 751 751 994 989 989 989 989 994 994

Big O for Radix Sort If number of digits is fixed, then we just need to do a fixed number of passes through n items Running time O(n) Works also for nonnumeric fixed-length sequences (e.g. fixed-length strings)

Sample problem Each of my friends is free for a different period of time on Saturday, e.g. 10am-1pm for person 1, 11am-5pm for person 2, 9:30am 10:30pm for person 3 12pm-4pm for person 4 What is the interval of time during which the most people are free?

One solution Convert to 24 hour time, and put all start and end times into a list, with each time tagged as start or end 10S,13E, 11S, 17E, 9.5S, 10.5E, 12S, 16E Sort times using any sorting algorithm 9.5S, 10S, 10.5E, 11S, 12S, 13E, 16E, 17E Move left to right, keeping track of #S - #E (this is the number of people free during this time) Whenever we reach a new maximum of #S - #E, record the current time, and set end time at next E Runs in O(n log n) time (or O(n) if times are integers)

Assignment: Mode of a list Generate a random list 10,000 integers between 1-100 Write a program that finds the mode (the most common number) of the list What is the time complexity of your algorithm? Note: you can use an existing implementation of a sorting algorithm Also: vote for rescheduling December 10th class: December 7th (Mon) or 11th (Fri)