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

07 b sorting ii
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

Recap: Sorting Heapsort Mergesort

07 B: Sorting II

CS1102S: Data Structures and Algorithms

Martin Henz

March 5, 2010

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

slide-2
SLIDE 2

Recap: Sorting Heapsort Mergesort

1

Recap: Sorting

2

Heapsort

3

Mergesort

CS1102S: Data Structures and Algorithms 07 B: Sorting II 2

slide-3
SLIDE 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

slide-4
SLIDE 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

slide-5
SLIDE 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

slide-6
SLIDE 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

slide-7
SLIDE 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

slide-8
SLIDE 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=0

i = N(N − 1)/2

CS1102S: Data Structures and Algorithms 07 B: Sorting II 8

slide-9
SLIDE 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 Lr. Consider a pair of elements (x, y) Either (x, y) is an inversion in L, or in Lr! Overall The sum of inversions of L and Lr 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

slide-10
SLIDE 10

Recap: Sorting Heapsort Mergesort

Runtime of Swapping Sorting Algorithms

Theorem Any algorithm that sorts its elements by swapping neighboring elements runs in Ω(N2). Theorem Any algorithm that removes one inversion in each step runs in Θ(N2).

CS1102S: Data Structures and Algorithms 07 B: Sorting II 10

slide-11
SLIDE 11

Recap: Sorting Heapsort Mergesort

Shell Sort: Idea

Main idea Proceed in passes h1, h2, . . . , ht, making sure that after each pass, a[i] ≤ a[i + hk]. Invariant After pass hk, elements are still hk+1 sorted

CS1102S: Data Structures and Algorithms 07 B: Sorting II 11

slide-12
SLIDE 12

Recap: Sorting Heapsort Mergesort

Shell Sort: Example using {1, 3, 5}

CS1102S: Data Structures and Algorithms 07 B: Sorting II 12

slide-13
SLIDE 13

Recap: Sorting Heapsort Mergesort

Analysis

Shell’s Increments The worst-case running time of Shellsort, using Shell’s increments 1, 2, 4, . . . ,, is Θ(N2). Hibbards’s Increments The worst-case running time of Shellsort, using Hibbard’s increments 1, 3, 7, . . . , 2k − 1, is Θ(N3/2).

CS1102S: Data Structures and Algorithms 07 B: Sorting II 13

slide-14
SLIDE 14

Recap: Sorting Heapsort Mergesort

1

Recap: Sorting

2

Heapsort

3

Mergesort

CS1102S: Data Structures and Algorithms 07 B: Sorting II 14

slide-15
SLIDE 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

slide-16
SLIDE 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

slide-17
SLIDE 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

slide-18
SLIDE 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

slide-19
SLIDE 19

Recap: Sorting Heapsort Mergesort

Heapsort

CS1102S: Data Structures and Algorithms 07 B: Sorting II 19

slide-20
SLIDE 20

Recap: Sorting Heapsort Mergesort

Heapsort

CS1102S: Data Structures and Algorithms 07 B: Sorting II 20

slide-21
SLIDE 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

slide-22
SLIDE 22

Recap: Sorting Heapsort Mergesort

Heapsort

private static <AnyType extends Comparable<? super AnyType> > void percDown ( AnyType [ ] a , int i , int n ) { 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

slide-23
SLIDE 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

slide-24
SLIDE 24

Recap: Sorting Heapsort Mergesort Idea Example Implementation

1

Recap: Sorting

2

Heapsort

3

Mergesort Idea Example Implementation

CS1102S: Data Structures and Algorithms 07 B: Sorting II 24

slide-25
SLIDE 25

Recap: Sorting Heapsort Mergesort Idea Example Implementation

Idea: Use recursion!

Split unsorted arrays into two halves

CS1102S: Data Structures and Algorithms 07 B: Sorting II 25

slide-26
SLIDE 26

Recap: Sorting Heapsort Mergesort Idea Example Implementation

Idea: Use recursion!

Split unsorted arrays into two halves Sort the two halves

CS1102S: Data Structures and Algorithms 07 B: Sorting II 26

slide-27
SLIDE 27

Recap: Sorting Heapsort Mergesort Idea Example 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

slide-28
SLIDE 28

Recap: Sorting Heapsort Mergesort Idea Example Implementation

Merging Two Sorted Arrays

Use two pointers, one for each sorted array

CS1102S: Data Structures and Algorithms 07 B: Sorting II 28

slide-29
SLIDE 29

Recap: Sorting Heapsort Mergesort Idea Example 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

slide-30
SLIDE 30

Recap: Sorting Heapsort Mergesort Idea Example 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

slide-31
SLIDE 31

Recap: Sorting Heapsort Mergesort Idea Example 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

slide-32
SLIDE 32

Recap: Sorting Heapsort Mergesort Idea Example Implementation

Example

Sort the array 26 13 1 14 15 38 2 27

CS1102S: Data Structures and Algorithms 07 B: Sorting II 32

slide-33
SLIDE 33

Recap: Sorting Heapsort Mergesort Idea Example Implementation

Implementation of Mergesort

public static <AnyType extends Comparable<? super AnyType> > void mergeSort ( AnyType [ ] a ) { AnyType [ ] tmpArray = ( AnyType [ ] ) new Comparable [ a . length ] ; mergeSort (a , tmpArray , 0 , a . length − 1 ) ; }

CS1102S: Data Structures and Algorithms 07 B: Sorting II 33

slide-34
SLIDE 34

Recap: Sorting Heapsort Mergesort Idea Example Implementation

Implementation of Mergesort

private static <AnyType extends Comparable<? super AnyType> > void mergeSort ( AnyType [ ] a , AnyType [ ] tmpArray , int l e f t , int r i g h t ) { i f ( l e f t < r i g h t ) { int center = ( l e f t + r i g h t ) / 2; mergeSort ( a , tmpArray , l e f t , center ) ; mergeSort ( a , tmpArray , center + 1 , r i g h t ) ; merge ( a , tmpArray , l e f t , center + 1 , r i g h t ) ; } }

CS1102S: Data Structures and Algorithms 07 B: Sorting II 34

slide-35
SLIDE 35

Recap: Sorting Heapsort Mergesort Idea Example Implementation

Implementation of Merge Operation

private static <AnyType extends Comparable<? super AnyType> > void merge ( AnyType [ ] a , AnyType [ ] tmpArray , int leftPos , int rightPos , int rightEnd ) { int leftEnd = rightPos − 1; int tmpPos = leftPos ; int numElements = rightEnd − leftPos + 1; while ( leftPos <= leftEnd && rightPos <= rightEnd ) i f ( a [ leftPos ] . compareTo ( a [ rightPos ] ) <= 0 ) tmpArray [ tmpPos++] = a [ leftPos ++]; else tmpArray [ tmpPos++] = a [ rightPos ++]; . . .

CS1102S: Data Structures and Algorithms 07 B: Sorting II 35

slide-36
SLIDE 36

Recap: Sorting Heapsort Mergesort Idea Example Implementation

Implementation of Merge Operation

private static <AnyType extends Comparable<? super AnyType> > void merge ( AnyType [ ] a , AnyType [ ] tmpArray , int leftPos , int rightPos , int rightEnd ) { . . . while ( leftPos <= leftEnd ) tmpArray [ tmpPos++] = a [ leftPos ++]; while ( rightPos <= rightEnd ) tmpArray [ tmpPos++] = a [ rightPos ++]; for ( int i = 0; i < numElements ; i ++, rightEnd −−) a [ rightEnd ] = tmpArray [ rightEnd ] ; }

CS1102S: Data Structures and Algorithms 07 B: Sorting II 36

slide-37
SLIDE 37

Recap: Sorting Heapsort Mergesort Idea Example Implementation

Next Week

Monday: Sit-in lab

CS1102S: Data Structures and Algorithms 07 B: Sorting II 37

slide-38
SLIDE 38

Recap: Sorting Heapsort Mergesort Idea Example Implementation

Next Week

Monday: Sit-in lab Wednesday lecture: Sorting III

CS1102S: Data Structures and Algorithms 07 B: Sorting II 38

slide-39
SLIDE 39

Recap: Sorting Heapsort Mergesort Idea Example Implementation

Next Week

Monday: Sit-in lab Wednesday lecture: Sorting III Friday: Midterm 2: Trees, Hashing, Priority Queues, Sorting I + II

CS1102S: Data Structures and Algorithms 07 B: Sorting II 39