CE 221 Data Structures and Algorithms Chapter 7: Sorting - - PowerPoint PPT Presentation

ce 221
SMART_READER_LITE
LIVE PREVIEW

CE 221 Data Structures and Algorithms Chapter 7: Sorting - - PowerPoint PPT Presentation

CE 221 Data Structures and Algorithms Chapter 7: Sorting (Heapsort, Mergesort) Text: Read Weiss, 7.5 7.6 Izmir University of Economics 1 Heapsort Priority queues can be used sort in O ( N log N ) time. First, build a binary


slide-1
SLIDE 1

1

Chapter 7: Sorting (Heapsort, Mergesort) CE 221 Data Structures and Algorithms

Izmir University of Economics

Text: Read Weiss, § 7.5 – 7.6

slide-2
SLIDE 2

2

Heapsort

  • Priority queues can be used sort in O(NlogN)

time. – First, build a binary heap of N elements in O(N) time – perform N deleteMin operations each taking O(logN) time, hence resulting in O(NlogN).

  • Problem: needs an extra array.
  • A clever solution: after each deleteMin heap

size shrinks by 1, use this space. But the result will be a decreasing sorted order. Thus we may use a max heap by changing the heap-order property

Izmir University of Economics

slide-3
SLIDE 3

Heapsort Example

Izmir University of Economics 3

Max heap after buildHeap phase Max heap after first deleteMax phase

slide-4
SLIDE 4

Analysis of Heapsort

  • worst case analysis
  • 2N comparisons-buildHeap
  • N-1 deleteMax operations.
  • Theorem: Average # of comparisons to heapsort a random

permutation of N distinct items is 2NlogN-O(NloglogN).

  • Proof: on any input, cost sequence D: d1, d2,..., dN

for any D, distinct deleteMax sequences total # of heaps with cost less than M is at most # of heaps with cost M < N(logN-loglogN-4) is at most (N/16)N. So the average # of comparisons is at least 2M.

Izmir University of Economics 4

 

) 2 ! ( ), ( log 2 ! log 2 log 2

2

N e N N N O N N N i

N N i

          

items N with heaps

  • f

number e N N f M s comparison

  • f

number d M Cost

N D N i i D

)) 4 /( ( ) ( 2 ,

1

   

 

D M D S N d d d D S 2 2 ... 2 2 1 2  

M N M i i N

N N 2 ) (log 2 ) (log

1 1

 

slide-5
SLIDE 5

5

Mergesort

  • runs in O(NlogN), # of comparisons is nearly
  • ptimal, fine example of a recursive algorithm.
  • Fundamental operation is merging of 2 sorted lists.
  • The basic merging algorithm takes 2 input arrays

A and B, an output array C. 3 counters, Actr, Bctr, and Cctr are initially set to the beginning of their respective arrays.

  • The smaller of A[Actr] and B[Bctr] is copied to

C[Cctr ] and the appropriate counters are advanced.

  • When either list is exhausted, the rest of the other

list is copied to C.

Izmir University of Economics

slide-6
SLIDE 6

Mergesort - merge

  • The time to merge is linear, at most N-1 comparisons are

required (since each comparison adds an element to C. It should also be noted that after N-1 elements are added to array C, the last element need not be compared but it simply gets copied).

  • Mergesort algorithm is then easy to describe.

If N=1 DONE else { mergesort(left half); mergesort(right half); merge(left half, right half); }

Izmir University of Economics 6

slide-7
SLIDE 7

7

Mergesort – Implementation - I

Izmir University of Economics

slide-8
SLIDE 8

Izmir University of Economics 8

Merge – Implementation - II

/* leftPos = start of left half, rightPos = start of right half Last copying may be avoided by interchanging the roles of the arrays a and tmpArray at alternate levels of recursion */

slide-9
SLIDE 9

Analysis of Mergesort

  • Write down recurrence relations and solve them

– T(1)=1 – T(N)=2T(N/2)+N // assumption is N=2k

Izmir University of Economics 9

) log ( log ) ( log 1 ) 1 ( ) ( ..... .......... .......... .......... 1 1 ) 1 ( 2 ) 2 ( 1 8 / ) 8 / ( 4 / ) 4 / ( 1 4 / ) 4 / ( 2 / ) 2 / ( 1 2 / ) 2 / ( ) ( N N O N N N N T N T N N T T T N N T N N T N N T N N T N N T N N T                N N N N T N N NT N T N k use kN N T N T N N T N T N N T N T N N T N T

k k

log ) ( log ) 1 ( ) ( log , ) 2 / ( 2 ) ( 3 ) 8 / ( 8 ) ( 2 ) 4 / ( 4 ) ( ) 2 / ( 2 ) (              

slide-10
SLIDE 10

Homework Assignments

  • 7.11, 7.12, 7.15, 7.17, 7.43 (7.38 in 3/e),

7.53 (7.48 in 3/e)

  • You are requested to study and solve the
  • exercises. Note that these are for you to

practice only. You are not to deliver the results to me.

Izmir University of Economics 10