Chapter 4, Part II Sorting Algorithms. 2 Heap Details A heap is a tree structure where for each subtree the value stored at the root is larger than all.

Slides:



Advertisements
Similar presentations
CS 400/600 – Data Structures External Sorting.
Advertisements

Introduction to Algorithms Quicksort
Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
Sorting Comparison-based algorithm review –You should know most of the algorithms –We will concentrate on their analyses –Special emphasis: Heapsort Lower.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
CS4413 Divide-and-Conquer
Chapter 4: Divide and Conquer Master Theorem, Mergesort, Quicksort, Binary Search, Binary Trees The Design and Analysis of Algorithms.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 17 Sorting.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 24 Sorting.
Data Structures Data Structures Topic #13. Today’s Agenda Sorting Algorithms: Recursive –mergesort –quicksort As we learn about each sorting algorithm,
Sorting Chapter Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.
1 Sorting Problem: Given a sequence of elements, find a permutation such that the resulting sequence is sorted in some order. We have already seen: –Insertion.
FALL 2004CENG 351 Data Management and File Structures1 External Sorting Reference: Chapter 8.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 19: Heap Sort.
FALL 2006CENG 351 Data Management and File Structures1 External Sorting.
Sorting. Introduction Assumptions –Sorting an array of integers –Entire sort can be done in main memory Straightforward algorithms are O(N 2 ) More complex.
CSC 2300 Data Structures & Algorithms March 20, 2007 Chapter 7. Sorting.
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.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L18 (Chapter 23) Algorithm.
Chapter 7 (Part 2) Sorting Algorithms Merge Sort.
CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University1 Sorting - 3 CS 202 – Fundamental Structures of Computer Science II.
Design and Analysis of Algorithms - Chapter 41 Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two.
External Sorting Problem: Sorting data sets too large to fit into main memory. –Assume data are stored on disk drive. To sort, portions of the data must.
1 Chapter 1 Analysis Basics. 2 Chapter Outline What is analysis? What to count and consider Mathematical background Rates of growth Tournament method.
1 Time Analysis Analyzing an algorithm = estimating the resources it requires. Time How long will it take to execute? Impossible to find exact value Depends.
CHAPTER 09 Compiled by: Dr. Mohammad Omar Alhawarat Sorting & Searching.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 19: Searching and Sorting Algorithms.
Merge Sort. What Is Sorting? To arrange a collection of items in some specified order. Numerical order Lexicographical order Input: sequence of numbers.
Chapter 10 B Algorithm Efficiency and Sorting. © 2004 Pearson Addison-Wesley. All rights reserved 9 A-2 Sorting Algorithms and Their Efficiency Sorting.
The Selection Problem. 2 Median and Order Statistics In this section, we will study algorithms for finding the i th smallest element in a set of n elements.
Adapted from instructor resource slides Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All.
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.
Data Structures Using C++ 2E Chapter 10 Sorting Algorithms.
© 2006 Pearson Addison-Wesley. All rights reserved10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 26 Sorting.
1 Sorting Algorithms Sections 7.1 to Comparison-Based Sorting Input – 2,3,1,15,11,23,1 Output – 1,1,2,3,11,15,23 Class ‘Animals’ – Sort Objects.
Sorting Chapter Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.
Algorithms and their Applications CS2004 ( ) Professor Jasna Kuljis (adapted from Dr Steve Swift) 6.1 Classic Algorithms - Sorting.
Chapter 18: Searching and Sorting Algorithms. Objectives In this chapter, you will: Learn the various search algorithms Implement sequential and binary.
Priority Queues and Heaps. October 2004John Edgar2  A queue should implement at least the first two of these operations:  insert – insert item at the.
Chapter 3 Searching and Selection Algorithms. 2 Chapter Outline Sequential search Binary search List element selection.
1 CSE 326: Data Structures A Sort of Detour Henry Kautz Winter Quarter 2002.
Chapter 8 Sorting and Searching Goals: 1.Java implementation of sorting algorithms 2.Selection and Insertion Sorts 3.Recursive Sorts: Mergesort and Quicksort.
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
Divide-and-Conquer The most-well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances 2.Solve smaller instances.
Sorting – Part II CS 367 – Introduction to Data Structures.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Internal and External Sorting External Searching
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 25 Sorting.
Sorting Fundamental Data Structures and Algorithms Aleks Nanevski February 17, 2004.
CSE 326: Data Structures Lecture 23 Spring Quarter 2001 Sorting, Part 1 David Kaplan
PREVIOUS SORTING ALGORITHMS  BUBBLE SORT –Time Complexity: O(n 2 ) For each item, make (n –1) comparisons Gives: Comparisons = (n –1) + (n – 2)
Week 13 - Wednesday.  What did we talk about last time?  NP-completeness.
Chapter 9: Sorting1 Sorting & Searching Ch. # 9. Chapter 9: Sorting2 Chapter Outline  What is sorting and complexity of sorting  Different types of.
Sorting Lower Bounds n Beating Them. Recap Divide and Conquer –Know how to break a problem into smaller problems, such that –Given a solution to the smaller.
Intro. to Data Structures Chapter 7 Sorting Veera Muangsin, Dept. of Computer Engineering, Chulalongkorn University 1 Chapter 7 Sorting Sort is.
Data Structures and Algorithms Instructor: Tesfaye Guta [M.Sc.] Haramaya University.
Chapter 4, Part I Sorting Algorithms. 2 Chapter Outline Insertion sort Bubble sort Shellsort Radix sort Heapsort Merge sort Quicksort External polyphase.
CENG 3511 External Sorting. CENG 3512 Outline Introduction Heapsort Multi-way Merging Multi-step merging Replacement Selection in heap-sort.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 26 Sorting.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 23 Sorting.
Chapter 23 Sorting Jung Soo (Sue) Lim Cal State LA.
Sorting.
Sorting With Priority Queue In-place Extra O(N) space
Lecture 3 / 4 Algorithm Analysis
Sub-Quadratic Sorting Algorithms
CSE 373 Data Structures and Algorithms
Quick-Sort 4/25/2019 8:10 AM Quick-Sort     2
CENG 351 Data Management and File Structures
Presentation transcript:

Chapter 4, Part II Sorting Algorithms

2 Heap Details A heap is a tree structure where for each subtree the value stored at the root is larger than all of the values stored in the subtree There is no ordering between the children of any node other than that they are smaller

3 Heap Details A heap is also a complete tree, so nodes are filled in along the bottom of the tree from left to right and a new level is started only when the previous level has been filled The largest value stored in a heap will be in the root of the heap and the smallest value will be in one of the leaves

4 Heap Example

5 Heapsort Heapsort begins by constructing a heap The root (the largest value in the heap) is moved to the last location of the list The heap is fixed and the process is repeated

6 Heap Storage We can store the heap using an array For an element at location i, its children will be in locations 2i and 2i+1 If 2i and 2i+1 are greater than the list size, then the element at location i is a leaf If only 2i+1 is greater than the list size, then the element at location i has just one child

7 Heap Construction Example

8 Final Heapsort Loop

9

10 Heapsort Algorithm construct the heap for i = 1 to N do copy the root to the list fix the heap end for

11 FixHeap Algorithm vacant = root while 2*vacant ≤ bound do largerChild = 2*vacant if (largerChild < bound) and (list[largerChild+1] > list[largerChild]) then largerChild = largerChild + 1 end if if key > list[ largerChild ] then break else list[ vacant ] = list[ largerChild ] vacant = largerChild end if end while list[ vacant ] = key

12 Constructing the Heap for i = N/2 down to 1 do FixHeap( list, i, list[ i ], N ) end for

13 The Final Heapsort Algorithm for i = N/2 down to 1 do FixHeap( list, i, list[ i ], N ) end for for i = N down to 2 do max = list[ 1 ] FixHeap( list, 1, list[ i ], i-1 ) list[ i ] = max end for

14 Worst-Case Analysis We analyze FixHeap because the rest of the analysis depends on it For each level of the heap, FixHeap does two comparisons – one between the children and the other between the new value and the largest child For a heap with D levels, there will be at most 2D comparisons

15 Worst-Case Analysis During heap construction, FixHeap is called N/2 times On the first pass, the heap will have depth of 1 On the last pass, the heap will have depth of lg N We need to determine how many nodes there are on each of the levels

16 Worst-Case Analysis For binary trees, we know that there is 1 node on the first level, 2 nodes on the second level, 4 nodes on the third level, and so on Putting this together gives:

17 Worst-Case Analysis In the second loop, the size of the heap decreases by one each pass If there are k nodes left in the heap, then the heap will have a depth of  lg k  This gives:

18 Worst-Case Analysis Overall, the worst case is given by:

19 Best-Case Analysis In the best case, the elements will be in the array in reverse order The construction phase will still be of O(N) Once the heap is constructed, the main loop will take the same O(N lg N) work So, the best case for heapsort is also O(N lg N)

20 Average-Case Analysis Average case must be between the best case and the worst case The average case for heapsort must be O(N lg N), because best and worst case are both O(N lg N)

21 Merge Sort If you have two sorted lists, you can create a combined sorted list if you merge the lists We know that the smallest value will be the first one in either of the two lists If we move the smallest value to the new list, we can repeat the process until the entire list is created

22 Merge Sort Example

23 Merge Sort Example (continued)

24 Merge Sort Example (continued)

25 The Algorithm if first < last then middle = ( first + last ) / 2 MergeSort( list, first, middle ) MergeSort( list, middle + 1, last ) MergeLists( list, first, middle, middle + 1, last ) end if

26 MergeList Algorithm Part 1 finalStart = start1 finalEnd = end2 indexC = 1 while (start1 ≤ end1) and (start2 ≤ end2) do if list[start1] < list[start2] then result[indexC] = list[start1] start1 = start1 + 1 else result[indexC] = list[start2] start2 = start2 + 1 end if indexC = indexC + 1 end while

27 MergeList Algorithm Part 2 if start1 ≤ end1 then for i = start1 to end1 do result[indexC] = list[i] indexC = indexC + 1 end for else for i = start2 to end2 do result[indexC] = list[i] indexC = indexC + 1 end for end if

28 MergeList Algorithm Part 3 indexC = 1 for i = finalStart to finalEnd do list[i] = result[indexC] indexC = indexC + 1 end for

29 MergeLists Analysis The best case is when the elements of one list are larger than all of the elements of the other list One worst case is when the elements are interleaved If each list has N elements, we will do N comparisons in the best case, and 2N-1 comparisons in the worst case

30 MergeSort Analysis MergeSort divides the list in half each time, so the difference between the best and worst cases is how much work MergeList does In the analysis, we consider that a list of N elements gets broken into two lists of N/2 elements that are recursively sorted and then merged together

31 MergeSort Analysis The worst case is: W(N) = 2W(N/2) + N – 1 W(0) = W(1) = 0 which solves to W(N) = O(N lg N) The best case is: B(N) = 2B(N/2) + N/2 B(0) = B(1) = 0 which solves to B(N) = O(N lg N)

32 Quicksort In Chapter 3, we saw a partition process used to help us find the K th largest element in a list We now use the partitioning process to help us sort a list We now will apply the process to both parts of the list instead of just one of them

33 Quicksort Quicksort will partition a list into two pieces: –Those elements smaller than the pivot value –Those elements larger than the pivot value Quicksort is then called recursively on both pieces

34 Quicksort Example

35 Quicksort Example (continued)

36 Quicksort Algorithm if first < last then pivot = PivotList( list, first, last ) Quicksort( list, first, pivot-1 ) Quicksort( list, pivot+1, last ) end if

37 Partitioning Process The algorithm moves through the list comparing values to the pivot During this process, there are sections of elements as indicated below

38 PivotList Algorithm PivotValue = list[ first ] PivotPoint = first for index = first+1 to last do if list[ index ] < PivotValue then PivotPoint = PivotPoint + 1 Swap( list[ PivotPoint ], list[ index ] ) end if end for // move pivot value into correct place Swap( list[ first ], list[ PivotPoint ] ) return PivotPoint

39 Worst-Case Analysis In the worst case, PivotList will do N – 1 comparisons, but create one partition that has N – 1 elements and the other will have no elements Because it winds up just reducing the partition by one element each time, worst case is given by:

40 Average-Case Analysis In the average case, we need to consider all of the possible places where the pivot point winds up Because there are N – 1 comparisons done to partition the list, and there are N ways this can be done, we have:

41 Average-Case Analysis Algebra can be used to simplify this recurrence relation to: This will then solve to:

42 External Polyphase Merge Sort Used when the data to be sorted is so large it will not fit in the computer’s memory External files are used to hold partial results Read in as many records as possible into memory and then sort them using one of the other sorts Alternate writing these runs of sorted records to one of two files

43 External Polyphase Merge Sort Merge pairs of runs from the two files into one run that is twice the length To do this, the runs might have to be read into memory in pieces, but the entire two runs must be merged before moving onto the next pair of runs This doubles the run length and halves the number of runs

44 External Polyphase Merge Sort The bigger runs are written alternately between two new files The process continues to merge pairs of runs until the entire data set has been merged back into a single sorted file

45 Run Creation Algorithm CurrentFile = A while not at the end of the input file do read S records from the input file sort the S records write the records to file CurrentFile if CurrentFile == A then CurrentFile = B else CurrentFile = A end if end while

46 Run Merge Algorithm Size = S Input1 = A Input2 = B CurrentOutput = C while not done do ***Merge runs process on next slide** Size = Size * 2 if Input1 == A then Input1 = C Input2 = D CurrrentOutput = A Else Input1 = A Input2 = B CurrentOutput = C end if end while

47 Merge Runs Process while more runs this pass do Merge one run of length Size from file Input1 with one run of length Size from file Input2 sending output to CurrentOutput if CurrentOutput == A then CurrentOutput = B elseif CurrentOutput == B then CurrentOutput = A elseif CurrentOutput == C then CurrentOutput = D elseif CurrentOutput == D then CurrentOutput = C end if end while

48 Run Creation Analysis This analysis assumes that there are N elements in the list and that they are broken down into R runs of S elements (N = R * S) If we use an efficient sort to create the runs, each run will take O(S lg S) and there will be R of them for a total time of O(R * S * lg S) = O(N lg S)

49 Run Merging Analysis On the first pass, we have R runs of S elements, so there will be R/2 merges that can take up to 2S – 1 comparisons, which is R/2 * (2S – 1) = R*S – R/2 On the second pass, we will have R/2 runs of 2S elements, so there will be R/4 merges that can take up to 4S – 1 comparisons, which is R/4 * (4S – 1) = R*S – R/4

50 Run Merging Analysis There will be lg R passes of the merge phase, so that the complexity is given by:

51 External Polyphase Merge Sort Analysis Putting the run creation and run merging calculations together we find that the overall complexity is O(N lg N)