Topics Recursive Sorting Algorithms Divide and Conquer technique An O(NlogN) Sorting Alg. using a Heap making use of the heap properties STL Sorting F

Similar documents
Topics Applications Most Common Methods Serial Search Binary Search Search by Hashing (next lecture) Run-Time Analysis Average-time analysis Time anal

We can use a max-heap to sort data.

Lecture 18 Heaps and Priority Queues

Overview of Presentation. Heapsort. Heap Properties. What is Heap? Building a Heap. Two Basic Procedure on Heap

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

Sorting. Order in the court! sorting 1

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

DATA STRUCTURES AND ALGORITHMS

Sorting. Order in the court! sorting 1

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

Merge Sort. Algorithm Analysis. November 15, 2017 Hassan Khosravi / Geoffrey Tien 1

Sorting Algorithms. + Analysis of the Sorting Algorithms

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

Overview of Sorting Algorithms

COMP Data Structures

CSC212 Data Structure - Section FG

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

Data Structures and Algorithms

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

Introduction to Computers and Programming. Today

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

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

Sorting Pearson Education, Inc. All rights reserved.

CS 171: Introduction to Computer Science II. Quicksort

Heaps. A heap is a certain kind of complete binary tree.

SORTING. Comparison of Quadratic Sorts

algorithm evaluation Performance

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

Lecture 19 Sorting Goodrich, Tamassia

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

CS 206 Introduction to Computer Science II

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

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

CS 310 Advanced Data Structures and Algorithms

Question 7.11 Show how heapsort processes the input:

ITEC2620 Introduction to Data Structures

Faster Sorting Methods

Sorting. CPSC 259: Data Structures and Algorithms for Electrical Engineers. Hassan Khosravi

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

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

Objectives. Chapter 23 Sorting. Why study sorting? What data to sort? Insertion Sort. CS1: Java Programming Colorado State University

Comparison Sorts. Chapter 9.4, 12.1, 12.2

Lecture 6 Sorting and Searching

Sorting is ordering a list of objects. Here are some sorting algorithms

CSC 273 Data Structures

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

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

8/5/10 TODAY'S OUTLINE. Recursion COMP 10 EXPLORING COMPUTER SCIENCE. Revisit search and sorting using recursion. Recursion WHAT DOES THIS CODE DO?

Sorting and Searching

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

ADT Priority Queue. Heaps. A Heap Implementation of the ADT Priority Queue. Heapsort

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

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

MERGESORT & QUICKSORT cs2420 Introduction to Algorithms and Data Structures Spring 2015

CSC Design and Analysis of Algorithms

Internal Sort by Comparison II

CMPSCI 187: Programming With Data Structures. Lecture #34: Efficient Sorting Algorithms David Mix Barrington 3 December 2012

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

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

CSC Design and Analysis of Algorithms. Lecture 6. Divide and Conquer Algorithm Design Technique. Divide-and-Conquer

Computer Science 385 Design and Analysis of Algorithms Siena College Spring Lab 8: Greedy Algorithms Due: Start of your next lab session

Sorting and Searching

Internal Sort by Comparison II

COS 226 Midterm Exam, Spring 2009

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

1) What is the primary purpose of template functions? 2) Suppose bag is a template class, what is the syntax for declaring a bag b of integers?

Cpt S 122 Data Structures. Sorting

Internal Sort by Comparison II

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

CSC Design and Analysis of Algorithms. Lecture 8. Transform and Conquer II Algorithm Design Technique. Transform and Conquer

Sorting. Bubble sort method. Bubble sort properties. Quick sort method. Notes. Eugeniy E. Mikhailov. Lecture 27. Notes. Notes

CSCI 136 Data Structures & Advanced Programming. Lecture 14 Spring 2018 Profs Bill & Jon

Programming II (CS300)

Unit-2 Divide and conquer 2016

Quick Sort. CSE Data Structures May 15, 2002

Programming II (CS300)

Chapter 10 Sorting and Searching Algorithms

Computer Science 4U Unit 1. Programming Concepts and Skills Algorithms

Merge Sort Goodrich, Tamassia. Merge Sort 1

Better sorting algorithms (Weiss chapter )

CSE 3101: Introduction to the Design and Analysis of Algorithms. Office hours: Wed 4-6 pm (CSEB 3043), or by appointment.

Merge Sort Goodrich, Tamassia Merge Sort 1

Sorting. Weiss chapter , 8.6

Lecture 5: Sorting Part A

About this exam review

Lecture Notes on Quicksort

CHAPTER 7 Iris Hui-Ru Jiang Fall 2008

FINALTERM EXAMINATION Fall 2009 CS301- Data Structures Question No: 1 ( Marks: 1 ) - Please choose one The data of the problem is of 2GB and the hard

4. Sorting and Order-Statistics

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

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

Sorting Goodrich, Tamassia Sorting 1

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

CSE 373 MAY 24 TH ANALYSIS AND NON- COMPARISON SORTING

CS106X Programming Abstractions in C++ Dr. Cynthia Bailey Lee

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

Lecture 8: Mergesort / Quicksort Steven Skiena

L14 Quicksort and Performance Optimization

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

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

Transcription:

CSC212 Data Structure t Lecture 21 Recursive Sorting, Heapsort & STL Quicksort Instructor: George Wolberg Department of Computer Science City College of New York @ George Wolberg, 2016 1

Topics Recursive Sorting Algorithms Divide and Conquer technique An O(NlogN) Sorting Alg. using a Heap making use of the heap properties STL Sorting Functions C++ sort function Original C version of qsort @ George Wolberg, 2016 2

The Divide-and-Conquer Technique Basic Idea: If the problem is small, simply solve it. Otherwise, divide the problem into two smaller sub-problems, each of which is about half of the original problem Solve each sub-problem, and then Combine the solutions of the sub-problems @ George Wolberg, 2016 3

The Divide-and-Conquer Sorting Paradigm 1. Divide the elements to be sorted into two groups of (almost) equal size 2. Sort each of these smaller groups of elements (by recursive calls) 3. Combine the two sorted groups into one large sorted list @ George Wolberg, 2016 4

Mergesort Divide the array in the middle Sort the two half-arrays arrays by recursion Merge the two halves void mergesort(int data[ ], size_t n) { size_t n1; // Size of the first subarray size_t n2; // Size of the second subarray } if (n > 1) { // Compute sizes of the subarrays. n1 = n / 2; n2 = n - n1; } // Sort from data[0] through data[n1-1] 1] mergesort(data, n1); // Sort from data[n1] to the end mergesort((data + n1), n2); // Merge the two sorted halves. merge(data, n1, n2); @ George Wolberg, 2016 5

Mergesort an Example @ George Wolberg, 2016 6

Mergesort an Example? 2 3 6 7 10 12 16 18 @ George Wolberg, 2016 7

Mergesort an Example divide @ George Wolberg, 2016 8

Mergesort an Example divide divide @ George Wolberg, 2016 9

Mergesort an Example divide divide divide @ George Wolberg, 2016 10

Mergesort an Example divide divide divide merge 12 16 6 7 2 3 10 18 @ George Wolberg, 2016 11

Mergesort an Example divide divide divide merge merge 12 16 6 7 2 3 10 18 6 7 12 16 2 3 10 18 @ George Wolberg, 2016 12

Mergesort an Example divide divide divide merge merge merge 12 16 6 7 2 3 10 18 6 7 12 16 2 3 10 18 2 3 6 7 10 12 16 18 @ George Wolberg, 2016 13

Mergesort two issues Specifying a subarray with pointer arithmetic int data[10]; (data+i)[0] is the same as data[i] (data+i][1] is the same as data[i+1] Merging two sorted subarrays into a sorted list need a temporary array (by new and then delete) step through the two sub-arrays with two cursors, and copy the elements in the right order @ George Wolberg, 2016 14

Mergesort - merge data 6 7 12 16 2 3 10 18 c1 c2 temp???????? d @ George Wolberg, 2016 15

Mergesort - merge data 6 7 12 16 2 3 10 18 c1 c2 temp 2??????? d @ George Wolberg, 2016 16

Mergesort - merge data 6 7 12 16 2 3 10 18 c1 c2 temp 2 3?????? d @ George Wolberg, 2016 17

Mergesort - merge data 6 7 12 16 2 3 10 18 c1 c2 temp 2 3 6????? @ George Wolberg, 2016 18 d

Mergesort - merge data 6 7 12 16 2 3 10 18 c1 c2 temp 2 3 6 7???? @ George Wolberg, 2016 19 d

Mergesort - merge data 6 7 12 16 2 3 10 18 c1 c2 temp 2 3 6 7 10??? @ George Wolberg, 2016 20 d

Mergesort - merge data 6 7 12 16 2 3 10 18 c1 c2 temp 2 3 6 7 10 12?? @ George Wolberg, 2016 21 d

Mergesort - merge data 6 7 12 16 2 3 10 18 c1 c2 temp 2 3 6 7 10 12 16? @ George Wolberg, 2016 22 d

Mergesort - merge data 6 7 12 16 2 3 10 18 c1 c2 temp 2 3 6 7 10 12 16 18 @ George Wolberg, 2016 23 d

Mergesort - merge data 2 3 6 7 10 12 16 18 temp 2 3 6 7 10 12 16 18 @ George Wolberg, 2016 24

Mergesort - merge data 2 3 6 7 10 12 16 18 @ George Wolberg, 2016 25

Mergesort Time Analysis The worst-case running time, the average- case running time and the best-case running time for mergesort are all O(n log n) @ George Wolberg, 2016 26

Mergesort an Example divide divide divide merge merge merge 12 16 6 7 2 3 10 18 6 7 12 16 2 3 10 18 2 3 6 7 10 12 16 18 @ George Wolberg, 2016 27

Mergesort Time Analysis At the top (0) level, 1 call to merge creates an array with n elements At the 1 st level, 2 calls to merge creates 2 arrays, each with n/2 elements At the 2 nd level, 4 calls to merge creates 4 arrays, each with n/4 elements At the 3 rd level, 8 calls to merge creates 8 arrays, each with n/8 elements At the dth level, 2 d calls to merge creates 2 d arrays, each with n/2 d elements Each level does total work proportional to n => c n, where c is a constant Assume at the dth level, the size of the subarrays is n/2 d =1, which means all the work is done at this level, therefore the number of levels d = log 2 n The total cost of the mergesort is c nd = c n log 2 n therefore the Big-O is O(n log 2 n) @ George Wolberg, 2016 28

Heapsort Heapsort Why a Heap? (two properties) Heapsort How to? (two steps) Heapsort How good? (time analysis) @ George Wolberg, 2016 29

Heap Definition A heap is a binary tree where the entries of the nodes can be compared with the less than operator of a strict weak ordering. In addition, two rules are followed: The entry contained by the node is NEVER less than the entries of the node s children The tree is a COMPLETE tree. @ George Wolberg, 2016 30

Why a Heap for Sorting? Two properties The largest element is always at the root Adding and removing an entry from a heap is O(log n) @ George Wolberg, 2016 31

Heapsort Basic Idea Step 1. Make a heap from elements add an entry to the heap one at a time reheapification upward n times O(n log n) Step 2. Make a sorted list from the heap Remove the root of the heap to a sorted list and Reheapification downward to re-organize into an updated heap n times O(n log n) @ George Wolberg, 2016 32

Heapsort Step 1: Make a Heap add an entry to the heap one at a time @ George Wolberg, 2016 33

Heapsort Step 1: Make a Heap 16 add an entry to the heap one at a time @ George Wolberg, 2016 34

Heapsort Step 1: Make a Heap 16 12 add an entry to the heap one at a time @ George Wolberg, 2016 35

Heapsort Step 1: Make a Heap 16 12 7 add an entry to the heap one at a time @ George Wolberg, 2016 36

Heapsort Step 1: Make a Heap 16 12 7 add an entry to the heap one at a time 6 @ George Wolberg, 2016 37

Heapsort Step 1: Make a Heap 16 12 7 add an entry to the heap one at a time 6 3 @ George Wolberg, 2016 38

Heapsort Step 1: Make a Heap 16 12 7 add an entry to the heap one at a time 6 3 2 @ George Wolberg, 2016 39

Heapsort Step 1: Make a Heap 16 12 7 add an entry to the heap one at a time 6 3 2 18 reheapification upward: push the out-of-place node upward @ George Wolberg, 2016 40

Heapsort Step 1: Make a Heap 16 12 18 6 3 2 7 10 16 12 18 add an entry to the heap one at a time 6 3 2 7 reheapification upward: push the out-of-place node upward @ George Wolberg, 2016 41

Heapsort Step 1: Make a Heap 18 12 16 6 3 2 7 10 18 12 16 add an entry to the heap one at a time 6 3 2 7 reheapification upward: push the out-of-place node upward until it is in the right place @ George Wolberg, 2016 42

Heapsort Step 1: Make a Heap 18 12 16 6 3 2 7 10 18 12 16 add an entry to the heap one at a time 10 6 3 2 7 reheapification upward: push the out-of of-place node upward until it is in the right place @ George Wolberg, 2016 43

Heapsort Step 1: Make a Heap 18 12 16 10 3 2 7 6 18 12 16 add an entry to the heap one at a time 6 10 3 2 7 reheapification upward: push the out-of of-place node upward until it is in the right place @ George Wolberg, 2016 44

Heapsort Step 1: Make a Heap 18 12 16 10 3 2 7 6 18 12 16 A heap is created: it is saved in the original array- the tree on the right is only for illustration! 6 10 3 2 7 Sorted??? @ George Wolberg, 2016 45

Heapsort Step 2: Sorting from Heap 18 12 16 10 3 2 7 6 18 12 16 heap -> sorted list from smallest to 6 largest Q: where is the largest entry? 10 3 2 7 @ George Wolberg, 2016 46

Heapsort Step 2: Sorting from Heap 18 12 16 10 3 2 7 6 18 12 16 Idea: remove the root of the heap and place it in the sorted list => recall: how to remove the root? 6 10 3 2 7 @ George Wolberg, 2016 47

Heapsort Step 2: Sorting from Heap almost a heap... 6 12 16 10 3 2 7 18 sorted side 6 12 16 How to remove the root? 10 3 2 7 move the last entry in the root... and for the sake of sorting, put the root entry in the sorted side @ George Wolberg, 2016 48

Heapsort Step 2: Sorting from Heap almost a heap... 6 12 16 10 3 2 7 18 sorted side 6 12 16 How to remove the root? 10 3 2 7 move the last entry in the root... then reposition the out-of of place node to update the heap @ George Wolberg, 2016 49

Heapsort Step 2: Sorting from Heap almost a heap... 16 12 6 10 3 2 7 18 sorted side 16 12 6 How to remove the root? 10 3 2 7 move the last entry in the root... then reposition the out-of of place node to update the heap reheapification downward @ George Wolberg, 2016 50

Heapsort Step 2: Sorting from Heap a heap in the unsorted side 16 12 7 10 3 2 6 18 sorted side 16 12 7 How to remove the root? 10 3 2 6 move the last entry in the root... then reposition the out-of of place node to update the heap reheapification downward @ George Wolberg, 2016 51

Heapsort Step 2: Sorting from Heap a heap in the unsorted side 16 12 7 10 3 2 6 18 sorted side 16 12 7 do the same thing again for the heap in the unsorted side until all the entries have been moved to the sorted side 10 3 2 6 @ George Wolberg, 2016 52

Heapsort Step 2: Sorting from Heap almost a heap... sorted side 6 12 7 10 3 2 16 18 6 12 7 do the same thing again for the heap in the unsorted side until all the entries have been moved to the sorted side 10 3 2 @ George Wolberg, 2016 53

Heapsort Step 2: Sorting from Heap almost a heap... sorted side 12 6 7 10 3 2 16 18 12 6 7 do the same thing again for the heap in the unsorted side until all the entries have been moved to the sorted side 10 3 2 @ George Wolberg, 2016 54

Heapsort Step 2: Sorting from Heap almost a heap... sorted side 12 10 7 6 3 2 16 18 12 10 7 do the same thing again for the heap in the unsorted side until all the entries have been moved to the sorted side 6 3 2 @ George Wolberg, 2016 55

Heapsort Step 2: Sorting from Heap a heap again! sorted side 12 10 7 6 3 2 16 18 12 10 7 do the same thing again for the heap in the unsorted side until all the entries have been moved to the sorted side 6 3 2 @ George Wolberg, 2016 56

Heapsort Step 2: Sorting from Heap almost a heap... sorted side 2 10 7 6 3 12 16 18 2 10 7 do the same thing again for the heap in the unsorted side until all the entries have been moved to the sorted side 6 3 @ George Wolberg, 2016 57

Heapsort Step 2: Sorting from Heap almost a heap... sorted side 10 2 7 6 3 12 16 18 10 2 7 do the same thing again for the heap in the unsorted side until all the entries have been moved to the sorted side 6 3 @ George Wolberg, 2016 58

Heapsort Step 2: Sorting from Heap a heap again! sorted side 10 6 7 2 3 12 16 18 10 6 7 do the same thing again for the heap in the unsorted side until all the entries have been moved to the sorted side 2 3 @ George Wolberg, 2016 59

Heapsort Step 2: Sorting from Heap almost a heap... sorted side 3 6 7 2 10 12 16 18 3 6 7 do the same thing again for the heap in the unsorted side until all the entries have been moved to the sorted side 2 @ George Wolberg, 2016 60

Heapsort Step 2: Sorting from Heap a heap again! sorted side 7 6 3 2 10 12 16 18 7 6 3 do the same thing again for the heap in the unsorted side until all the entries have been moved to the sorted side 2 @ George Wolberg, 2016 61

Heapsort Step 2: Sorting from Heap a heap?? sorted side 2 6 3 7 10 12 16 18 2 6 3 do the same thing again for the heap in the unsorted side until all the entries have been moved to the sorted side @ George Wolberg, 2016 62

Heapsort Step 2: Sorting from Heap a heap!! sorted side 6 2 3 7 10 12 16 18 6 2 3 do the same thing again for the heap in the unsorted side until all the entries have been moved to the sorted side @ George Wolberg, 2016 63

Heapsort Step 2: Sorting from Heap heap! sorted side 3 2 6 7 10 12 16 18 2 3 do the same thing again for the heap in the unsorted side until all the entries have been moved to the sorted side @ George Wolberg, 2016 64

Heapsort Step 2: Sorting from Heap heap! sorted side 2 3 6 7 10 12 16 18 2 do the same thing again for the heap in the unsorted side until all the entries have been moved to the sorted side @ George Wolberg, 2016 65

Heapsort Step 2: Sorting from Heap sorted side 2 3 6 7 10 12 16 18 do the same thing again for the heap in the unsorted side until all the entries have been moved to the sorted side DONE! @ George Wolberg, 2016 66

Heapsort Time Analysis Step 1. Make a heap from elements add an entry to the heap one at a time reheapification upward n times O(n log n) Step 2. Make a sorted list from the heap Remove the root of the heap to a sorted list and Reheapification downward to re-organize the unsorted side into an updated heap do this n times O(n log n) The running time is O(n log n) @ George Wolberg, 2016 67

C++ STL Sorting Functions The C++ sort function void sort(iterator begin, Iterator end); The original C version of qsort void qsort( ); void *base, size_t number_of_elements, size_t element_size, int compare(const void*, const void*) @ George Wolberg, 2016 68

Summary & Homework Recursive Sorting Algorithms Divide and Conquer technique An O(NlogN) Sorting Algorithm using a Heap making use of the heap properties STL Sorting Functions C++ sort function Original C version of qsort Homework use your heap implementation to implement a heapsort! @ George Wolberg, 2016 69