CSC212 Data Structure - Section RS

Slides:



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

Garfield AP Computer Science
CS 206 Introduction to Computer Science II 03 / 23 / 2009 Instructor: Michael Eckmann.
Sorting Algorithms Bryce Boe 2012/08/13 CS32, Summer 2012 B.
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 22 Recursive Sorting, Heapsort & STL Quicksort Instructor: Zhigang Zhu Department.
@ Zhigang Zhu, CSC212 Data Structure - Section FG Lecture 19 Searching Instructor: Zhigang Zhu Department of Computer Science City College.
CS 206 Introduction to Computer Science II 10 / 31 / 2008 Happy Halloween!!! Instructor: Michael Eckmann.
Chapter 10 Heaps Anshuman Razdan Div of Computing Studies
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.
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.
Merge Sort. What Is Sorting? To arrange a collection of items in some specified order. Numerical order Lexicographical order Input: sequence of numbers.
Heapsort. Heapsort is a comparison-based sorting algorithm, and is part of the selection sort family. Although somewhat slower in practice on most machines.
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.
CSC211 Data Structures Lecture 18 Heaps and Priority Queues Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.
1 CSC212 Data Structure - Section AB Lecture 22 Recursive Sorting, Heapsort & STL Quicksort Instructor: Edgardo Molina Department of Computer Science City.
Heaps and Heapsort Prof. Sin-Min Lee Department of Computer Science San Jose State University.
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.
@ Zhigang Zhu, CSC212 Data Structure - Section FG Lecture 17 B-Trees and the Set Class Instructor: Zhigang Zhu Department of Computer Science.
Priority Queues and Heaps. John Edgar  Define the ADT priority queue  Define the partially ordered property  Define a heap  Implement a heap using.
Algorithm Design Techniques, Greedy Method – Knapsack Problem, Job Sequencing, Divide and Conquer Method – Quick Sort, Finding Maximum and Minimum, Dynamic.
Chapter 23 Sorting Jung Soo (Sue) Lim Cal State LA.
Chapter 24 Sorting.
Algorithms Sorting – Part 3.
Fundamental Data Structures and Algorithms
CSC212 Data Structure - Section AB
CS 162 Intro to Programming II
Heapsort CSE 373 Data Structures.
Sorting Algorithms CENG 213 Data Structures 1.
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.
COMP 103 Sorting with Binary Trees: Tree sort, Heap sort Alex Potanin
COMP 103 HeapSort Thomas Kuehne 2013-T1 Lecture 27
Design and Analysis of Algorithms
Divide and Conquer.
Sorting means The values stored in an array have keys of a type for which the relational operators are defined. (We also assume unique keys.) Sorting.
Chapter 4: Divide and Conquer
CSC212 Data Structure - Section RS
i206: Lecture 14: Heaps, Graphs intro.
Heap Sort The idea: build a heap containing the elements to be sorted, then remove them in order. Let n be the size of the heap, and m be the number of.
Balanced-Trees This presentation shows you the potential problem of unbalanced tree and show two way to fix it This lecture introduces heaps, which are.
Hassan Khosravi / Geoffrey Tien
CSC212 Data Structure - Section AB
B-Trees This presentation shows you the potential problem of unbalanced tree and show one way to fix it This lecture introduces heaps, which are used.
Lecture No 6 Advance Analysis of Institute of Southern Punjab Multan
Heaps Chapter 11 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates.
C++ Plus Data Structures
Balanced-Trees This presentation shows you the potential problem of unbalanced tree and show two way to fix it This lecture introduces heaps, which are.
Yan Shi CS/SE 2630 Lecture Notes
Sub-Quadratic Sorting Algorithms
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.
CSE 326: Data Structures Sorting
Heapsort CSE 373 Data Structures.
Sorting And Searching CSE116A,B 4/6/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
CO4301 – Advanced Games Development Week 4 Binary Search Trees
CSCE 3110 Data Structures & Algorithm Analysis
CSCE 3110 Data Structures & Algorithm Analysis
B-Trees This presentation shows you the potential problem of unbalanced tree and show one way to fix it This lecture introduces heaps, which are used.
CSCE 3110 Data Structures & Algorithm Analysis
Heaps.
CMPT 225 Lecture 10 – Merge Sort.
Presentation transcript:

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

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

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

The Divide-and-Conquer Sorting Paradigm Divide the elements to be sorted into two groups of (almost) equal size Sort each of these smaller groups of elements (by recursive calls) Combine the two sorted groups into one large sorted list

Mergesort Divide the array in the middle 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] mergesort(data, n1); // Sort from data[n1] to the end mergesort((data + n1), n2); // Merge the two sorted halves. merge(data, n1, n2); } Divide the array in the middle Sort the two half-arrays by recursion Merge the two halves

Mergesort – an Example 16 12 7 6 3 2 18 10 [0] [1] [2] [3] [4] [5] [6] [7]

Mergesort – an Example 16 12 7 6 3 2 18 10 ? 2 3 6 7 10 12 16 18

Mergesort – an Example 16 12 7 6 3 2 18 10 divide 16 12 7 6 3 2 18 10

Mergesort – an Example 16 12 7 6 3 2 18 10 divide 16 12 7 6 3 2 18 10 16 12 7 6 3 2 18 10 divide

Mergesort – an Example 16 12 7 6 3 2 18 10 divide 16 12 7 6 3 2 18 10 16 12 7 6 3 2 18 10 divide divide 16 12 7 6 3 2 18 10

Mergesort – an Example 16 12 7 6 3 2 18 10 divide 16 12 7 6 3 2 18 10 16 12 7 6 3 2 18 10 divide divide 16 12 7 6 3 2 18 10 merge 12 16 6 7 2 3 10 18

Mergesort – an Example 16 12 7 6 3 2 18 10 divide 16 12 7 6 3 2 18 10 16 12 7 6 3 2 18 10 divide divide 16 12 7 6 3 2 18 10 merge 12 16 6 7 2 3 10 18 6 7 12 16 2 3 10 18 merge

Mergesort – an Example 16 12 7 6 3 2 18 10 divide 16 12 7 6 3 2 18 10 16 12 7 6 3 2 18 10 divide divide 16 12 7 6 3 2 18 10 merge 12 16 6 7 2 3 10 18 6 7 12 16 2 3 10 18 merge merge 2 3 6 7 10 12 16 18

Mergesort – two issues Specifying a subarray with pointer arithmetic int data[10]; (data+i)[0] is the same of 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

Mergesort - merge 6 7 12 16 2 3 10 18 data [0] [1] [2] [3] [4] [5] [6] [7] c1 c2 temp ? [0] [1] [2] [3] [4] [5] [6] [7] d

Mergesort - merge 6 7 12 16 2 3 10 18 data [0] [1] [2] [3] [4] [5] [6] [7] c1 c2 temp 2 ? [0] [1] [2] [3] [4] [5] [6] [7] d

Mergesort - merge 6 7 12 16 2 3 10 18 data [0] [1] [2] [3] [4] [5] [6] [7] c1 c2 temp 2 3 ? [0] [1] [2] [3] [4] [5] [6] [7] d

Mergesort - merge 6 7 12 16 2 3 10 18 data [0] [1] [2] [3] [4] [5] [6] [7] c1 c2 temp 2 3 6 ? [0] [1] [2] [3] [4] [5] [6] [7] d

Mergesort - merge 6 7 12 16 2 3 10 18 data [0] [1] [2] [3] [4] [5] [6] [7] c2 c1 temp 2 3 6 7 ? [0] [1] [2] [3] [4] [5] [6] [7] d

Mergesort - merge 6 7 12 16 2 3 10 18 data [0] [1] [2] [3] [4] [5] [6] [7] c2 c1 temp 2 3 6 7 10 ? [0] [1] [2] [3] [4] [5] [6] [7] d

Mergesort - merge 6 7 12 16 2 3 10 18 data [0] [1] [2] [3] [4] [5] [6] [7] c2 c1 temp 2 3 6 7 10 12 ? [0] [1] [2] [3] [4] [5] [6] [7] d

Mergesort - merge 6 7 12 16 2 3 10 18 data [0] [1] [2] [3] [4] [5] [6] [7] c1 c2 temp 2 3 6 7 10 12 16 ? [0] [1] [2] [3] [4] [5] [6] [7] d

Mergesort - merge 6 7 12 16 2 3 10 18 data [0] [1] [2] [3] [4] [5] [6] [7] c1 c2 temp 2 3 6 7 10 12 16 18 [0] [1] [2] [3] [4] [5] [6] [7] d

Mergesort - merge 2 3 6 7 10 12 16 18 data [0] [1] [2] [3] [4] [5] [6] [7] temp 2 3 6 7 10 12 16 18 [0] [1] [2] [3] [4] [5] [6] [7]

Mergesort - merge 2 3 6 7 10 12 16 18 data [0] [1] [2] [3] [4] [5] [6] [7]

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)

Mergesort – an Example 16 12 7 6 3 2 18 10 divide 16 12 7 6 3 2 18 10 16 12 7 6 3 2 18 10 divide divide 16 12 7 6 3 2 18 10 merge 12 16 6 7 2 3 10 18 6 7 12 16 2 3 10 18 merge merge 2 3 6 7 10 12 16 18

Mergesort – Time Analysis At the top (0) level, 1 call to merge creates an array with n elements At the 1st level, 2 calls to merge creates 2 arrays, each with n/2 elements At the 2nd level, 4 calls to merge creates 4 arrays, each with n/4 elements At the 3rd level, 8 calls to merge creates 8 arrays, each with n/8 elements At the dth level, 2d calls to merge creates 2d arrays, each with n/2d 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/2d =1, which means all the work is done at this level, therefore the number of levels d = log2 n The total cost of the mergesort is c nd = c n log2 n therefore the Big-O is O(n log2 n)

Heapsort Heapsort – Why a Heap? (two properties) Hepasort – How to? (two steps) Heapsort – How good? (time analysis)

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. Complete binary tree Every level except the deepest level must contains as many nodes as possible, and at the deepest level, all the nodes are as far left as possible

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)

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 a updated heap n times – O(n log n)

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

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

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

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

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

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

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

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

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

Heapsort – Step 1: Make a Heap 18 18 12 16 6 3 2 7 10 [0] [1] [2] [3] [4] [5] [6] [7] 12 16 6 3 2 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

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

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

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

Heapsort – Step 2: Sorting from Heap 18 18 12 16 10 3 2 7 6 [0] [1] [2] [3] [4] [5] [6] [7] 12 16 10 3 2 7 heap -> sorted list from smallest to largest Q: where is the largest entry? 6

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

Heapsort – Step 2: Sorting from Heap almost a heap... sorted side 6 6 12 16 10 3 2 7 18 [0] [1] [2] [3] [4] [5] [6] [7] 12 16 10 3 2 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” 7

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

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

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

Heapsort – Step 2: Sorting from Heap a heap in the unsorted side sorted side 16 16 12 7 10 3 2 6 18 [0] [1] [2] [3] [4] [5] [6] [7] 12 7 10 3 2 do the same thing again for the heap in the unsorted side until all the entries have been moved to the sorted side 6

Heapsort – Step 2: Sorting from Heap almost a heap... sorted side 6 6 12 7 10 3 2 16 18 [0] [1] [2] [3] [4] [5] [6] [7] 12 7 10 3 2 do the same thing again for the heap in the unsorted side until all the entries have been moved to the sorted side

Heapsort – Step 2: Sorting from Heap almost a heap... sorted side 12 12 6 7 10 3 2 16 18 [0] [1] [2] [3] [4] [5] [6] [7] 6 7 10 3 2 do the same thing again for the heap in the unsorted side until all the entries have been moved to the sorted side

Heapsort – Step 2: Sorting from Heap almost a heap... sorted side 12 12 10 7 6 3 2 16 18 [0] [1] [2] [3] [4] [5] [6] [7] 10 7 6 3 2 do the same thing again for the heap in the unsorted side until all the entries have been moved to the sorted side

Heapsort – Step 2: Sorting from Heap a heap again! sorted side 12 12 10 7 6 3 2 16 18 [0] [1] [2] [3] [4] [5] [6] [7] 10 7 6 3 2 do the same thing again for the heap in the unsorted side until all the entries have been moved to the sorted side

Heapsort – Step 2: Sorting from Heap almost a heap... sorted side 2 2 10 7 6 3 12 16 18 [0] [1] [2] [3] [4] [5] [6] [7] 10 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

Heapsort – Step 2: Sorting from Heap almost a heap... sorted side 10 10 2 7 6 3 12 16 18 [0] [1] [2] [3] [4] [5] [6] [7] 2 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

Heapsort – Step 2: Sorting from Heap a heap again! sorted side 10 10 6 7 2 3 12 16 18 [0] [1] [2] [3] [4] [5] [6] [7] 6 7 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

Heapsort – Step 2: Sorting from Heap almost a heap... sorted side 3 3 6 7 2 10 12 16 18 [0] [1] [2] [3] [4] [5] [6] [7] 6 7 2 do the same thing again for the heap in the unsorted side until all the entries have been moved to the sorted side

Heapsort – Step 2: Sorting from Heap a heap again ! sorted side 7 7 6 3 2 10 12 16 18 [0] [1] [2] [3] [4] [5] [6] [7] 6 3 2 do the same thing again for the heap in the unsorted side until all the entries have been moved to the sorted side

Heapsort – Step 2: Sorting from Heap a heap ?? sorted side 2 2 6 3 7 10 12 16 18 [0] [1] [2] [3] [4] [5] [6] [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

Heapsort – Step 2: Sorting from Heap a heap !! sorted side 6 6 2 3 7 10 12 16 18 [0] [1] [2] [3] [4] [5] [6] [7] 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

Heapsort – Step 2: Sorting from Heap sorted side 3 3 2 6 7 10 12 16 18 [0] [1] [2] [3] [4] [5] [6] [7] 2 do the same thing again for the heap in the unsorted side until all the entries have been moved to the sorted side

Heapsort – Step 2: Sorting from Heap sorted side 2 2 3 6 7 10 12 16 18 [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

Heapsort – Step 2: Sorting from Heap sorted side 2 3 6 7 10 12 16 18 [0] [1] [2] [3] [4] [5] [6] [7] DONE! do the same thing again for the heap in the unsorted side until all the entries have been moved to the sorted side

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 a updated heap do this n times – O(n log n) The running time is O(n log n) To be more accurate, the total numbers of operations in both Step 1 and Step 2 are proportional to T(n) = log 1 +log 2 +log 3 + … + log (n/2) + … + log n Apparently we have T(n) < log n + log n + … + log n = n log n We also have T(n) > log (n/2) + log (n/2+1)+…+log n // only the second n/1 items > log (n/2) + log (n/2) + … + log(n/2) // each item is replaced with the smallest one among them = (n/2) log(n/2) = 0.5 n log n – 0.5 n Therefore we have 0.5 n log n – 0.5 n < T(n) < n log n Which leads to the result that the Big-O of the heapsort algorithm is O(n log n) #

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*) );

Summary & Homework 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 Homework use your heap implementation to implement a heapsort!