07 b sorting ii
play

07 B: Sorting II CS1102S: Data Structures and Algorithms Martin - PowerPoint PPT Presentation

Recap: Sorting Heapsort Mergesort 07 B: Sorting II CS1102S: Data Structures and Algorithms Martin Henz March 5, 2010 Generated on Friday 5 th March, 2010, 08:31 CS1102S: Data Structures and Algorithms 07 B: Sorting II 1 Recap: Sorting


  1. Recap: Sorting Heapsort Mergesort 07 B: Sorting II CS1102S: Data Structures and Algorithms Martin Henz March 5, 2010 Generated on Friday 5 th March, 2010, 08:31 CS1102S: Data Structures and Algorithms 07 B: Sorting II 1

  2. Recap: Sorting Heapsort Mergesort 1 Recap: Sorting 2 Heapsort 3 Mergesort CS1102S: Data Structures and Algorithms 07 B: Sorting II 2

  3. Recap: Sorting Heapsort Mergesort Sorting Input Unsorted array of elements Behavior Rearrange elements of array such that the smallest appears first, followed by the second smallest etc, finally followed by the largest element CS1102S: Data Structures and Algorithms 07 B: Sorting II 3

  4. Recap: Sorting Heapsort Mergesort Comparison-based Sorting The only requirement A comparison function for elements The only operation Comparisons are the only operations allowed on elements CS1102S: Data Structures and Algorithms 07 B: Sorting II 4

  5. Recap: Sorting Heapsort Mergesort Insertion Sort: Idea Passes Algorithm proceeds in N − 1 passes Invariant After pass i , the elements in positions 0 to i are sorted. Consequence of Invariant After N − 1 passes, the elements in positions 0 to N − 1 are sorted. CS1102S: Data Structures and Algorithms 07 B: Sorting II 5

  6. Recap: Sorting Heapsort Mergesort Insertion Sort: Idea Passes Algorithm proceeds in N − 1 passes Invariant After pass i , the elements in positions 0 to i are sorted. Consequence of Invariant After N − 1 passes, the elements in positions 0 to N − 1 are sorted. That is the whole array! CS1102S: Data Structures and Algorithms 07 B: Sorting II 6

  7. Recap: Sorting Heapsort Mergesort How to do a pass? Pass i Move element in position i to the left, until it is larger than the element to the left or until it is at the beginning of the array. CS1102S: Data Structures and Algorithms 07 B: Sorting II 7

  8. Recap: Sorting Heapsort Mergesort Worst Case How many inversions in the worst case? A list sorted in reverse has the maximal number of inversions Maximal number of inversions N − 1 i = N ( N − 1 ) / 2 � i = 0 CS1102S: Data Structures and Algorithms 07 B: Sorting II 8

  9. Recap: Sorting Heapsort Mergesort Average Case How many inversions in the average case? Consider the number of inversions in an list L and its reverse L r . Consider a pair of elements ( x , y ) Either ( x , y ) is an inversion in L , or in L r ! Overall The sum of inversions of L and L r together is N ( N − 1 ) / 2. Overall average The overall average of inversions in a given list is N ( N − 1 ) / 4 CS1102S: Data Structures and Algorithms 07 B: Sorting II 9

  10. Recap: Sorting Heapsort Mergesort Runtime of Swapping Sorting Algorithms Theorem Any algorithm that sorts its elements by swapping neighboring elements runs in Ω( N 2 ) . Theorem Any algorithm that removes one inversion in each step runs in Θ( N 2 ) . CS1102S: Data Structures and Algorithms 07 B: Sorting II 10

  11. Recap: Sorting Heapsort Mergesort Shell Sort: Idea Main idea Proceed in passes h 1 , h 2 , . . . , h t , making sure that after each pass, a [ i ] ≤ a [ i + h k ] . Invariant After pass h k , elements are still h k + 1 sorted CS1102S: Data Structures and Algorithms 07 B: Sorting II 11

  12. Recap: Sorting Heapsort Mergesort Shell Sort: Example using { 1 , 3 , 5 } CS1102S: Data Structures and Algorithms 07 B: Sorting II 12

  13. Recap: Sorting Heapsort Mergesort Analysis Shell’s Increments The worst-case running time of Shellsort, using Shell’s increments 1 , 2 , 4 , . . . , , is Θ( N 2 ) . Hibbards’s Increments The worst-case running time of Shellsort, using Hibbard’s increments 1 , 3 , 7 , . . . , 2 k − 1, is Θ( N 3 / 2 ) . CS1102S: Data Structures and Algorithms 07 B: Sorting II 13

  14. Recap: Sorting Heapsort Mergesort 1 Recap: Sorting 2 Heapsort 3 Mergesort CS1102S: Data Structures and Algorithms 07 B: Sorting II 14

  15. Recap: Sorting Heapsort Mergesort Idea Use heap to sort Build heap from unsorted array (using percolateDown) CS1102S: Data Structures and Algorithms 07 B: Sorting II 15

  16. Recap: Sorting Heapsort Mergesort Idea Use heap to sort Build heap from unsorted array (using percolateDown) Repeatedly take minimal element (using deleteMin) and place it in sorted array CS1102S: Data Structures and Algorithms 07 B: Sorting II 16

  17. Recap: Sorting Heapsort Mergesort Idea Use heap to sort Build heap from unsorted array (using percolateDown) Repeatedly take minimal element (using deleteMin) and place it in sorted array Drawback Will require extra array CS1102S: Data Structures and Algorithms 07 B: Sorting II 17

  18. Recap: Sorting Heapsort Mergesort Idea Use heap to sort Build heap from unsorted array (using percolateDown) Repeatedly take minimal element (using deleteMin) and place it in sorted array Drawback Will require extra array How to avoid this? Use free memory at the end of the heap! CS1102S: Data Structures and Algorithms 07 B: Sorting II 18

  19. Recap: Sorting Heapsort Mergesort Heapsort CS1102S: Data Structures and Algorithms 07 B: Sorting II 19

  20. Recap: Sorting Heapsort Mergesort Heapsort CS1102S: Data Structures and Algorithms 07 B: Sorting II 20

  21. Recap: Sorting Heapsort Mergesort Heapsort private static int l e f t C h i l d ( int i ) { return 2 ∗ i + 1; } CS1102S: Data Structures and Algorithms 07 B: Sorting II 21

  22. Recap: Sorting Heapsort Mergesort Heapsort private static < AnyType extends Comparable < ? super AnyType > > void percDown ( AnyType [ int int n ) { ] a , i , int c h i l d ; AnyType tmp ; for ( tmp = a [ i ] ; l e f t C h i l d ( i ) < n ; i = c h i l d ) { c h i l d = l e f t C h i l d ( i ) ; i f ( c h i l d != n − 1 && a [ c h i l d ] . compareTo ( a [ c h i l d + 1 ] ) < 0) c h i l d ++; i f ( tmp . compareTo ( a [ c h i l d ] ) < 0) a [ i ] = a [ c h i l d ] ; else break ; } a [ i ] = tmp ; } CS1102S: Data Structures and Algorithms 07 B: Sorting II 22

  23. Recap: Sorting Heapsort Mergesort Heapsort public static < AnyType extends Comparable < ? super AnyType > > void heapsort ( AnyType [ ] a ) { for ( int i = a . length / 2; i > = 0; i −− ) percDown ( a , i , a . length ) ; for ( int i = a . length − 1; i > 0; i −− ) { swapReferences ( a , 0 , i ) ; percDown ( a , 0 , i ) ; } } CS1102S: Data Structures and Algorithms 07 B: Sorting II 23

  24. Recap: Sorting Idea Heapsort Example Mergesort Implementation 1 Recap: Sorting 2 Heapsort 3 Mergesort Idea Example Implementation CS1102S: Data Structures and Algorithms 07 B: Sorting II 24

  25. Recap: Sorting Idea Heapsort Example Mergesort Implementation Idea: Use recursion! Split unsorted arrays into two halves CS1102S: Data Structures and Algorithms 07 B: Sorting II 25

  26. Recap: Sorting Idea Heapsort Example Mergesort Implementation Idea: Use recursion! Split unsorted arrays into two halves Sort the two halves CS1102S: Data Structures and Algorithms 07 B: Sorting II 26

  27. Recap: Sorting Idea Heapsort Example Mergesort Implementation Idea: Use recursion! Split unsorted arrays into two halves Sort the two halves Merge the two sorted halves CS1102S: Data Structures and Algorithms 07 B: Sorting II 27

  28. Recap: Sorting Idea Heapsort Example Mergesort Implementation Merging Two Sorted Arrays Use two pointers, one for each sorted array CS1102S: Data Structures and Algorithms 07 B: Sorting II 28

  29. Recap: Sorting Idea Heapsort Example Mergesort Implementation Merging Two Sorted Arrays Use two pointers, one for each sorted array Compare values at pointer positions CS1102S: Data Structures and Algorithms 07 B: Sorting II 29

  30. Recap: Sorting Idea Heapsort Example Mergesort Implementation Merging Two Sorted Arrays Use two pointers, one for each sorted array Compare values at pointer positions Copy the smaller values into sorted array CS1102S: Data Structures and Algorithms 07 B: Sorting II 30

  31. Recap: Sorting Idea Heapsort Example Mergesort Implementation Merging Two Sorted Arrays Use two pointers, one for each sorted array Compare values at pointer positions Copy the smaller values into sorted array Advance the pointer that pointed at smaller value CS1102S: Data Structures and Algorithms 07 B: Sorting II 31

  32. Recap: Sorting Idea Heapsort Example Mergesort Implementation Example Sort the array 26 13 1 14 15 38 2 27 CS1102S: Data Structures and Algorithms 07 B: Sorting II 32

  33. Recap: Sorting Idea Heapsort Example Mergesort Implementation Implementation of Mergesort public static < AnyType extends Comparable < ? super AnyType > > void mergeSort ( AnyType [ ] a ) { AnyType [ ] tmpArray = ( AnyType [ ] ) new Comparable [ a . length ] ; 0 , a . length − 1 mergeSort (a , tmpArray , ) ; } CS1102S: Data Structures and Algorithms 07 B: Sorting II 33

Recommend


More recommend