@ Zhigang Zhu, 2004-2014 1 CSC212 Data Structure - Section FG Lecture 22 Recursive Sorting, Heapsort & STL Quicksort Instructor: Zhigang Zhu Department.

Slides:



Advertisements
Similar presentations
Exam Review 3 Chapters 10 – 13, 15 CSC212 Section FG CS Dept, CCNY.
Advertisements

Back to Sorting – More efficient sorting algorithms.
Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
CS 206 Introduction to Computer Science II 03 / 23 / 2009 Instructor: Michael Eckmann.
Sorting Algorithms Bryce Boe 2012/08/13 CS32, Summer 2012 B.
1 Merge Sort Review of Sorting Merge Sort. 2 Sorting Algorithms Selection Sort uses a priority queue P implemented with an unsorted sequence: –Phase 1:
Stephen P. Carl - CS 2421 Recursive Sorting Algorithms Reading: Chapter 5.
Chapter 4: Divide and Conquer Master Theorem, Mergesort, Quicksort, Binary Search, Binary Trees The Design and Analysis of Algorithms.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 5.
Analysis of Algorithms CS 477/677 Sorting – Part B Instructor: George Bebis (Chapter 7)
CSC2100B Quick Sort and Merge Sort Xin 1. Quick Sort Efficient sorting algorithm Example of Divide and Conquer algorithm Two phases ◦ Partition phase.
Sorting Algorithms and Average Case Time Complexity
@ Zhigang Zhu, CSC212 Data Structure - Section FG Lecture 19 Searching Instructor: Zhigang Zhu Department of Computer Science City College.
Exam Review 3 Chapters 10 – 13, 15 CSC212 Section FG CS Dept, CCNY.
CS 253: Algorithms Chapter 7 Mergesort Quicksort Credit: Dr. George Bebis.
Sorting21 Recursive sorting algorithms Oh no, not again!
1 Sorting/Searching CS308 Data Structures. 2 Sorting means... l Sorting rearranges the elements into either ascending or descending order within the array.
CS 206 Introduction to Computer Science II 10 / 31 / 2008 Happy Halloween!!! Instructor: Michael Eckmann.
Divide-and-Conquer1 7 2  9 4   2  2 79  4   72  29  94  4.
@ Zhigang Zhu, CSC212 Data Structure - Section RS Lecture 18a Trees, Logs and Time Analysis Instructor: Zhigang Zhu Department of Computer.
C++ Plus Data Structures
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.
sorting31 Sorting III: Heapsort sorting32 A good sorting algorithm is hard to find... Quadratic sorting algorithms (with running times of O(N 2 ), such.
CSE 373 Data Structures Lecture 19
Sorting (Part II: Divide and Conquer) CSE 373 Data Structures Lecture 14.
Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.
(c) , University of Washington
1 HEAPS & PRIORITY QUEUES Array and Tree implementations.
CSCE 3110 Data Structures & Algorithm Analysis Sorting (I) Reading: Chap.7, Weiss.
Merge Sort. What Is Sorting? To arrange a collection of items in some specified order. Numerical order Lexicographical order Input: sequence of numbers.
Lecture 2 Sorting. Sorting Problem Insertion Sort, Merge Sort e.g.,
Heapsort. Heapsort is a comparison-based sorting algorithm, and is part of the selection sort family. Although somewhat slower in practice on most machines.
P p Chapter 10 has several programming projects, including a project that uses heaps. p p This presentation shows you what a heap is, and demonstrates.
Sorting. Pseudocode of Insertion Sort Insertion Sort To sort array A[0..n-1], sort A[0..n-2] recursively and then insert A[n-1] in its proper place among.
Searching and Sorting Recursion, Merge-sort, Divide & Conquer, Bucket sort, Radix sort Lecture 5.
Java Methods Big-O Analysis of Algorithms Object-Oriented Programming
CSC211 Data Structures Lecture 18 Heaps and Priority Queues Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.
Chapter 8 Sorting and Searching Goals: 1.Java implementation of sorting algorithms 2.Selection and Insertion Sorts 3.Recursive Sorts: Mergesort and Quicksort.
CSS106 Introduction to Elementary Algorithms M.Sc Askar Satabaldiyev Lecture 05: MergeSort & QuickSort.
1 CSC212 Data Structure - Section AB Lecture 22 Recursive Sorting, Heapsort & STL Quicksort Instructor: Edgardo Molina Department of Computer Science City.
CENG 213 Data Structures Sorting Algorithms. CENG 213 Data Structures Sorting Sorting is a process that organizes a collection of data into either ascending.
Heaps and Heapsort Prof. Sin-Min Lee Department of Computer Science San Jose State University.
INTRO TO HEAP & Adding and Removing a NODE Presented to: Sir AHSAN RAZA Presented by: SHAH RUKH Roll #07-22 Semester BIT 3 rd Session 2007—2011 Department.
CSC212 Data Structure Lecture 16 Heaps and Priority Queues Instructor: George Wolberg Department of Computer Science City College of New York.
Sorting – Part II CS 367 – Introduction to Data Structures.
Sorting Fundamental Data Structures and Algorithms Aleks Nanevski February 17, 2004.
1 Ch.19 Divide and Conquer. 2 BIRD’S-EYE VIEW Divide and conquer algorithms Decompose a problem instance into several smaller independent instances May.
PREVIOUS SORTING ALGORITHMS  BUBBLE SORT –Time Complexity: O(n 2 ) For each item, make (n –1) comparisons Gives: Comparisons = (n –1) + (n – 2)
Lecture # 6 1 Advance Analysis of Algorithms. Divide-and-Conquer Divide the problem into a number of subproblems Similar sub-problems of smaller size.
@ Zhigang Zhu, CSC212 Data Structure - Section FG Lecture 17 B-Trees and the Set Class Instructor: Zhigang Zhu Department of Computer Science.
Data Structures and Algorithms Instructor: Tesfaye Guta [M.Sc.] Haramaya University.
BITS Pilani Pilani Campus Data Structure and Algorithms Design Dr. Maheswari Karthikeyan Lecture3.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 23 Sorting.
CSC212 Data Structure - Section AB
Quick Sort and Merge Sort
Sorting Chapter 13 presents several common algorithms for sorting an array of integers. Two slow but simple algorithms are Selectionsort and Insertionsort.
Divide and Conquer.
CSC212 Data Structure - Section RS
CSC212 Data Structure - Section RS
CSC212 Data Structure - Section AB
Heaps Chapter 11 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates.
Sorting Chapter 13 presents several common algorithms for sorting an array of integers. Two slow but simple algorithms are Selectionsort and Insertionsort.
Sorting And Searching CSE116A,B 2/23/2019 B.Ramamurthy.
Sorting And Searching CSE116A,B 4/7/2019 B.Ramamurthy.
CSC212 Data Structure - Section KL
Chapter 10 Sorting Algorithms
Searching/Sorting/Searching
Heaps.
Presentation transcript:

@ Zhigang Zhu, CSC212 Data Structure - Section FG Lecture 22 Recursive Sorting, Heapsort & STL Quicksort Instructor: Zhigang Zhu Department of Computer Science City College of New York

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

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

@ Zhigang Zhu, 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

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

@ Zhigang Zhu, Mergesort – an Example [0] [1] [2] [3] [4] [5] [6] [7]

@ Zhigang Zhu, Mergesort – an Example ?

@ Zhigang Zhu, Mergesort – an Example divide

@ Zhigang Zhu, Mergesort – an Example divide divide

@ Zhigang Zhu, Mergesort – an Example divide divide divide

@ Zhigang Zhu, Mergesort – an Example divide divide divide merge

@ Zhigang Zhu, Mergesort – an Example divide divide divide merge merge

@ Zhigang Zhu, Mergesort – an Example divide divide divide merge merge merge

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

@ Zhigang Zhu, Mergesort - merge ????????data temp [0] [1] [2] [3] [4] [5] [6] [7] c1c2 d

@ Zhigang Zhu, Mergesort - merge ???????data temp [0] [1] [2] [3] [4] [5] [6] [7] c1c2 d

@ Zhigang Zhu, Mergesort - merge ??????data temp [0] [1] [2] [3] [4] [5] [6] [7] c1c2 d

@ Zhigang Zhu, Mergesort - merge ?????data temp [0] [1] [2] [3] [4] [5] [6] [7] c1c2 d

@ Zhigang Zhu, Mergesort - merge ????data temp [0] [1] [2] [3] [4] [5] [6] [7] c1 c2 d

@ Zhigang Zhu, Mergesort - merge ???data temp [0] [1] [2] [3] [4] [5] [6] [7] c1 c2 d

@ Zhigang Zhu, Mergesort - merge ??data temp [0] [1] [2] [3] [4] [5] [6] [7] c1 c2 d

@ Zhigang Zhu, Mergesort - merge ?data temp [0] [1] [2] [3] [4] [5] [6] [7] c1c2 d

@ Zhigang Zhu, Mergesort - merge data temp [0] [1] [2] [3] [4] [5] [6] [7] c1c2 d

@ Zhigang Zhu, Mergesort - merge data temp [0] [1] [2] [3] [4] [5] [6] [7]

@ Zhigang Zhu, Mergesort - merge data [0] [1] [2] [3] [4] [5] [6] [7]

@ Zhigang Zhu, void merge(int data[ ], size_t n1, size_t n2) // Precondition: The first n1 elements of data are sorted, and the // next n2 elements of data are sorted (from smallest to largest). // Postcondition: The n1+n2 elements of data are now completely sorted. QUIZ

@ Zhigang Zhu, Mergesort – Time Analysis p The worst-case running time, the average- case running time and the best-case running time for mergesort are all O(n log n)

@ Zhigang Zhu, Mergesort – an Example divide divide divide merge merge merge

@ Zhigang Zhu, Mergesort – Time Analysis p At the top (0) level, 1 call to merge creates an array with n elements p At the 1 st level, 2 calls to merge creates 2 arrays, each with n/2 elements p At the 2 nd level, 4 calls to merge creates 4 arrays, each with n/4 elements p At the 3 rd level, 8 calls to merge creates 8 arrays, each with n/8 elements p At the dth level, 2 d calls to merge creates 2 d arrays, each with n/2 d elements p Each level does total work proportional to n => c n, where c is a constant p 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 p the number of levels d = log 2 n p The total cost of the mergesort is c nd = c n log 2 n p therefore the Big-O is O(n log 2 n)

@ Zhigang Zhu, Heapsort p Heapsort – Why a Heap? (two properties) p Hepasort – How to? (two steps) p Heapsort – How good? (time analysis)

@ Zhigang Zhu, Heap Definition p 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. p In addition, two rules are followed: p The entry contained by the node is NEVER less than the entries of the node’s children p The tree is a COMPLETE tree.

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

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

@ Zhigang Zhu, Heapsort – Step 1: Make a Heap [0] [1] [2] [3] [4] [5] [6] [7] add an entry to the heap one at a time

@ Zhigang Zhu, Heapsort – Step 1: Make a Heap [0] [1] [2] [3] [4] [5] [6] [7] add an entry to the heap one at a time 16

@ Zhigang Zhu, Heapsort – Step 1: Make a Heap [0] [1] [2] [3] [4] [5] [6] [7] add an entry to the heap one at a time 16 12

@ Zhigang Zhu, Heapsort – Step 1: Make a Heap [0] [1] [2] [3] [4] [5] [6] [7] add an entry to the heap one at a time

@ Zhigang Zhu, Heapsort – Step 1: Make a Heap [0] [1] [2] [3] [4] [5] [6] [7] add an entry to the heap one at a time

@ Zhigang Zhu, Heapsort – Step 1: Make a Heap [0] [1] [2] [3] [4] [5] [6] [7] add an entry to the heap one at a time

@ Zhigang Zhu, Heapsort – Step 1: Make a Heap [0] [1] [2] [3] [4] [5] [6] [7] add an entry to the heap one at a time

@ Zhigang Zhu, Heapsort – Step 1: Make a Heap [0] [1] [2] [3] [4] [5] [6] [7] add an entry to the heap one at a time reheapification upward: push the out-of-place node upward

@ Zhigang Zhu, Heapsort – Step 1: Make a Heap [0] [1] [2] [3] [4] [5] [6] [7] add an entry to the heap one at a time reheapification upward: push the out-of-place node upward

@ Zhigang Zhu, Heapsort – Step 1: Make a Heap [0] [1] [2] [3] [4] [5] [6] [7] add an entry to the heap one at a time reheapification upward: push the out-of-place node upward until it is in the right place

@ Zhigang Zhu, Heapsort – Step 1: Make a Heap [0] [1] [2] [3] [4] [5] [6] [7] add an entry to the heap one at a time reheapification upward: push the out-of-place node upward until it is in the right place

@ Zhigang Zhu, Heapsort – Step 1: Make a Heap add an entry to the heap one at a time reheapification upward: push the out-of-place node upward until it is in the right place [0] [1] [2] [3] [4] [5] [6] [7]

@ Zhigang Zhu, Heapsort – Step 1: Make a Heap A heap is created: it is saved in the original array- the tree on the right is only for illustration! [0] [1] [2] [3] [4] [5] [6] [7] Sorted???

@ Zhigang Zhu, Heapsort – Step 2: Sorting from Heap [0] [1] [2] [3] [4] [5] [6] [7] heap -> sorted list from smallest to largest Q: where is the largest entry?

@ Zhigang Zhu, Heapsort – Step 2: Sorting from Heap [0] [1] [2] [3] [4] [5] [6] [7] Idea: remove the root of the heap and place it in the sorted list => recall: how to remove the root?

@ Zhigang Zhu, Heapsort – Step 2: Sorting from Heap [0] [1] [2] [3] [4] [5] [6] [7] How to remove the root? move the last entry in the root... and for the sake of sorting, put the root entry in the “sorted side” almost a heap... sorted side

@ Zhigang Zhu, Heapsort – Step 2: Sorting from Heap [0] [1] [2] [3] [4] [5] [6] [7] How to remove the root? move the last entry in the root... then reposition the out-of place node to update the heap sorted side almost a heap...

@ Zhigang Zhu, Heapsort – Step 2: Sorting from Heap [0] [1] [2] [3] [4] [5] [6] [7] How to remove the root? move the last entry in the root... then reposition the out-of place node to update the heap sorted side reheapification downward almost a heap...

@ Zhigang Zhu, Heapsort – Step 2: Sorting from Heap [0] [1] [2] [3] [4] [5] [6] [7] How to remove the root? move the last entry in the root... then reposition the out-of place node to update the heap sorted side reheapification downward a heap in the unsorted side

@ Zhigang Zhu, Heapsort – Step 2: Sorting from Heap [0] [1] [2] [3] [4] [5] [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 sorted side a heap in the unsorted side

@ Zhigang Zhu, Heapsort – Step 2: Sorting from Heap [0] [1] [2] [3] [4] [5] [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 sorted side almost a heap...

@ Zhigang Zhu, Heapsort – Step 2: Sorting from Heap [0] [1] [2] [3] [4] [5] [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 sorted side almost a heap...

@ Zhigang Zhu, Heapsort – Step 2: Sorting from Heap [0] [1] [2] [3] [4] [5] [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 sorted side almost a heap...

@ Zhigang Zhu, Heapsort – Step 2: Sorting from Heap [0] [1] [2] [3] [4] [5] [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 sorted side a heap again!

@ Zhigang Zhu, Heapsort – Step 2: Sorting from Heap [0] [1] [2] [3] [4] [5] [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 sorted side almost a heap...

@ Zhigang Zhu, Heapsort – Step 2: Sorting from Heap [0] [1] [2] [3] [4] [5] [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 sorted side almost a heap...

@ Zhigang Zhu, Heapsort – Step 2: Sorting from Heap [0] [1] [2] [3] [4] [5] [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 sorted side a heap again!

@ Zhigang Zhu, Heapsort – Step 2: Sorting from Heap [0] [1] [2] [3] [4] [5] [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 sorted side almost a heap...

@ Zhigang Zhu, Heapsort – Step 2: Sorting from Heap [0] [1] [2] [3] [4] [5] [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 sorted side a heap again !

@ Zhigang Zhu, Heapsort – Step 2: Sorting from Heap [0] [1] [2] [3] [4] [5] [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 a heap ?? sorted side

@ Zhigang Zhu, Heapsort – Step 2: Sorting from Heap [0] [1] [2] [3] [4] [5] [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 sorted side a heap !!

@ Zhigang Zhu, Heapsort – Step 2: Sorting from Heap [0] [1] [2] [3] [4] [5] [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 sorted side heap !

@ Zhigang Zhu, Heapsort – Step 2: Sorting from Heap [0] [1] [2] [3] [4] [5] [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 sorted side heap !

@ Zhigang Zhu, Heapsort – Step 2: Sorting from Heap [0] [1] [2] [3] [4] [5] [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 sorted side DONE!

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

@ Zhigang Zhu, C++ STL Sorting Functions p The C++ sort function p void sort(Iterator begin, Iterator end); p 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*) );

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