lecture 2 sorting

76
Lecture 2 Sorting

Upload: iorwen

Post on 06-Feb-2016

36 views

Category:

Documents


2 download

DESCRIPTION

Lecture 2 Sorting. Sorting Problem. e.g.,. Insertion Sort, Merge Sort. Efficiency. Running time from receiving the input to producing the output. Running time. Insertion Sort. Merge Sort. Heapsort. Heap, a data structure Max-Heapify procedure Building a heap Heapsort. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Lecture 2  Sorting

Lecture 2 Sorting

Page 2: Lecture 2  Sorting

Sorting Problem

.'''such that sequence

input of }',...,','{n permutatioA :Output

}.,...,,{ numbers of sequenceA :Input

21

21

21

n

n

n

aaa

aaa

aaan

Insertion Sort, Merge Sort

6. 5, 4, 3, 2, 1, :Output

3. 1, 6, 4, 2, 5, :Input

e.g.,

Page 3: Lecture 2  Sorting

Efficiency

• Running time from receiving the input to producing the output.

)log(

)( 2

nnO

nOInsertion Sort

Merge Sort

Running time

Page 4: Lecture 2  Sorting

Is array a data structure?

Page 5: Lecture 2  Sorting

Is array a data structure?

• No!

• A data structure is a standard part in construction of algorithms.

• What data structures do you know on array?

Page 6: Lecture 2  Sorting

Is array a data structure?

• No!

• A data structure is a standard part in construction of algorithms.

• What data structures do you know on array?

• Stack, queue, list, …, heap.

Page 7: Lecture 2  Sorting

Heapsort

• Heap, a data structure

• Max-Heapify procedure

• Building a heap

• Heapsort

Page 8: Lecture 2  Sorting

A Data Structure Heap

• A heap is an array object that can be viewed as a nearly complete binary tree.

35

2 4 1

6

6 5 3 2 4 1

1

2 3

4 5 6

Page 9: Lecture 2  Sorting

;12return

)(Right

;2return

)(Left

;2/return

)(Parent

ii

i

i

i

i

Page 10: Lecture 2  Sorting

Max-Heap

].[)](Parent[

:property following thesatisfiesroot

theother than nodeevery heap,-max aIn

iAiA

i

Page 11: Lecture 2  Sorting

Min-Heap

].[)](Parent[

:property following thesatisfiesroot

theother than nodeevery heap,-min aIn

iAiA

i

Page 12: Lecture 2  Sorting

Max-Heapify

• Max-Heapify(A,i) is a subroutine.

• When it is called, two subtrees rooted at Left(i) and Right(i) are max-heaps, but A[i] may not satisfy the max-heap property.

• Max-Heapify(A,i) makes the subtree rooted at A[i] become a max-heap by letting A[i] “float down”.

Page 13: Lecture 2  Sorting

4

714

2 18

7

2 8 1

8 7

12

14

4

14

4

Page 14: Lecture 2  Sorting

if-end

);g,(Heapify-Max

];g[][ exchangebegin then

g if

;g then

]g[][ and ][ if

;g else

g then

][][ and ][ if

);(Right

);(Left

),(Heapify-Max

estlarA

estlarAiA

iestlar

restlar

estlarArAAsizeheapr

iestlar

lestlar

iAlAAsizeheapl

ir

il

iA

Page 15: Lecture 2  Sorting

Running time

).(lg in time runsHeapify -Max Hence,

][- wherelg

nO

Asizeheapnnheight

.lg Hence,

.2 is,that

,1221 satisfying

integer largest theis Then .Let 1

nh

n

n

hheighth

h

h

Page 16: Lecture 2  Sorting

Building a Heap

);,(Heapify-Max do

1 downto 2/][for

];[][-

)(Heap-Max-Build

iA

Alengthi

AlengthAsizeheap

A

e.g., 4, 1, 3, 2, 16, 9, 10, 14, 8, 7.

Page 17: Lecture 2  Sorting

4

31

16 10

7

2 9

14 8

.2/ is child a has holocation wlast The n

Proof. .on induction by is proof This n

n

2/n

Page 18: Lecture 2  Sorting

4

31

16 10

7

2 9

14 8

.2/1)/2(n even. is 1. Case nn

7

n 1n

Page 19: Lecture 2  Sorting

4

31

16 10

7

2 9

14 8

.12/1)/2(n odd. is 2. Case nn

n 1n

Page 20: Lecture 2  Sorting

4

31

16 10

7

2 9

14 8

.12/1)/2(n odd. is 2. Case nn

n 1n

Page 21: Lecture 2  Sorting

Building a Max-Heap

);,(Heapify-Max do

1 downto 2/][for

];[][-

)(Heap-Max-Build

iA

Alengthi

AlengthAsizeheap

A

e.g., 4, 1, 3, 2, 16, 9, 10, 14, 8, 7.

Page 22: Lecture 2  Sorting

4

31

16 10

7

2 9

14 8

Page 23: Lecture 2  Sorting

4

31

16 10

7

2 9

14 8

Page 24: Lecture 2  Sorting

4

31

16 10

7

14 9

2 8

Page 25: Lecture 2  Sorting

4

31

16 10

7

14 9

2 8

Page 26: Lecture 2  Sorting

4

1

16 3

7

14 9

2 8

10

Page 27: Lecture 2  Sorting

4

1

16 3

7

14 9

2 8

10

Page 28: Lecture 2  Sorting

4

16

1 3

7

14 9

2 8

10

Page 29: Lecture 2  Sorting

4

16

7 3

1

14 9

2 8

10

Page 30: Lecture 2  Sorting

16

4

7 3

1

14 9

2 8

10

Page 31: Lecture 2  Sorting

16

14

7 3

1

4 9

2 8

10

Page 32: Lecture 2  Sorting

16

14

7 3

1

8 9

2 4

10

Page 33: Lecture 2  Sorting

Analysis

).( and levelat

nodes ofnumber theis )(# where

)()(# timerunning0

Aheighthi

inode

ihOinodeh

i

iinode

Asizeheapnnh

2)(#

)(- wherelg

Page 34: Lecture 2  Sorting

16

14

7 3

1

8 9

2 4

10

0i

1i

hi

Page 35: Lecture 2  Sorting

Running time

).(2

,2)2/11(

2/1

2

because

)()2

)(2()(2)(2

20

000

nO

l

nOih

OihOihO

h

ll

h

iih

hh

i

ih

i

i

22/11

1

2

1

2

1

2

11

2/11

2/1

2/11

2/1

2/11

2/1

2/11

1/2

2

1

2

1

2

1

2

1

2

32

432

43210

k

kk

kk

kk

kl

l

l

.1for

10

q

q

aaq

k

k

Page 36: Lecture 2  Sorting

Heapsort

for-end

;)1(Heapify-Max

;1][-][-

];[]1[ exchange

begin do

2 downto ][for

);(Heap-Max-Buid

)(Heapsort

A,

AsizeheapAsizeheap

iAA

Alengthi

A

A

Page 37: Lecture 2  Sorting

16

14

7 3

1

8 9

2 4

10

Input: 4, 1, 3, 2, 16, 9, 10, 14, 8, 7.Build a max-heap

16, 14, 10, 8, 7, 9, 3, 2, 4, 1.

Page 38: Lecture 2  Sorting

1

14

7 3

16

8 9

2 4

10

Page 39: Lecture 2  Sorting

1

14

7 3

16

8 9

2 4

10

1, 14, 10, 8, 7, 9, 3, 2, 4, 16.

Page 40: Lecture 2  Sorting

14

8

7 3

16

4 9

2 1

10

Page 41: Lecture 2  Sorting

14

8

7 3

16

4 9

2 1

10

14, 8, 10, 4, 7, 9, 3, 2, 1, 16.

Page 42: Lecture 2  Sorting

1

8

7 3

16

4 9

2 14

10

Page 43: Lecture 2  Sorting

1

8

7 3

16

4 9

2 14

10

1, 8, 10, 4, 7, 9, 3, 2, 14, 16.

Page 44: Lecture 2  Sorting

10

8

7 3

16

4 1

2 14

9

Page 45: Lecture 2  Sorting

10

8

7 3

16

4 1

2 14

9

10, 8, 9, 4, 7, 1, 3, 2, 14, 16.

Page 46: Lecture 2  Sorting

2

8

7 3

16

4 1

10 14

9

Page 47: Lecture 2  Sorting

2

8

7 3

16

4 1

10 14

9

2, 8, 9, 4, 7, 1, 3, 10, 14, 16.

Page 48: Lecture 2  Sorting

9

8

7 2

16

4 1

10 14

3

Page 49: Lecture 2  Sorting

9

8

7 2

16

4 1

10 14

3

9, 8, 3, 4, 7, 1, 2, 10, 14, 16.

Page 50: Lecture 2  Sorting

2

8

7 9

16

4 1

10 14

3

Page 51: Lecture 2  Sorting

8

7

2 9

16

4 1

10 14

3

Page 52: Lecture 2  Sorting

1

7

2 9

16

4 8

10 14

3

Page 53: Lecture 2  Sorting

7

4

2 9

16

1 8

10 14

3

Page 54: Lecture 2  Sorting

2

4

7 9

16

1 8

10 14

3

Page 55: Lecture 2  Sorting

4

2

7 9

16

1 8

10 14

3

Page 56: Lecture 2  Sorting

1

2

7 9

16

4 8

10 14

3

Page 57: Lecture 2  Sorting

3

2

7 9

16

4 8

10 14

1

Page 58: Lecture 2  Sorting

1

2

7 9

16

4 8

10 14

3

Page 59: Lecture 2  Sorting

2

1

7 9

16

4 8

10 14

3

Page 60: Lecture 2  Sorting

1

2

7 9

16

4 8

10 14

3

Page 61: Lecture 2  Sorting

Running Time

for-end

;)1(Heapify-Max

;1][-][-

];[]1[ exchange

begin do

2 downto ][for

);(Heap-Max-Buid

)(Heapsort

A,

AsizeheapAsizeheap

iAA

Alengthi

A

A

O(lg n)

O(n)

)lg( nnO

Page 62: Lecture 2  Sorting

Quicksort

• Worst-case running time

• Expected running time

• The best practical choice (why?)

)( 2n

)lg( nnO

Page 63: Lecture 2  Sorting

Divide and Conquer

• Divide the problem into subproblems.

• Conquer the subproblems by solving them recursively.

• Combine the solutions to subproblems into the solution for original problem.

Page 64: Lecture 2  Sorting

Idea of Quicksort

them.combine toneeded is work no

],..1[ and ]1..[sort after Then

.1for ][][

1for ][][

such that ]..1[ and ]1..[

subarrays into ]..[array Partition

rqAqpA

riqiAqA

qipqAiA

rqAqpA

rpA

Page 65: Lecture 2  Sorting

Example

9. 8, 7, 6, ,5̂ 4, 3, 2, 1,

6. 7, 9, 8, ,5̂ 1, 2, 4, 3,

Page 66: Lecture 2  Sorting

Quicksort

then-end

);,1,(Quicksort

);1,,(Quicksort

);,,(Partition

beginn the

if

),,(Quicksort

rqA

qpA

rpAq

rp

rpA

Page 67: Lecture 2  Sorting

How to find such a partition?

• Such a partition may not exist.

e.g., 5, 4, 3, 2, 1.

• Hence, we may need to make such a partition.

• Take a A[i].

• Classify other A[j] by comparing it with A[i].

Page 68: Lecture 2  Sorting

8 6, 5, 7, ,4̂ 3, 1, 2,

4̂ 6, 5, 7, 8, ,3 1, 2,

4̂ ,6 5, 7, 8, ,3 1, 2,

4̂ 6, ,5 7, 8, ,3 1, 2,

4̂ 6, 5, ,3 8, 7, ,1 2,

4̂ 6, 5, 3, ,1 7, 8, ,2

4̂ 6, 5, 3, 1, 7, ,8 ,2

4̂ 6, 5, 3, 1, 7, 8, ,2

4̂ 6, 5, 3, 1, 7, 8, ,2 ,

Page 69: Lecture 2  Sorting

;1return

];[]1[ exchange

];[][ exchange and 1 then

][ if do

1 tofor

;1

];[

),,(Partition

i

rAiA

jAiAii

xjA

rpj

pi

rAx

rpA

)( timerunning n

Page 70: Lecture 2  Sorting

7̂ 6, 5, 8, ,4 ,1 3, 2,

4̂ 6, 5, 8, ,7 ,1 3, 2,

4̂ 6, 5, 8, ,7 ,1 3, 2,

4̂ 6, 5, 8, ,1 ,7 3, 2,

4̂ 6, 5, ,8 1, 7, ,3 2,

4̂ 6, 5, ,3 1, 7, ,8 2,

4̂ ,6 5, 3, 1, 7, 8, ,2

Page 71: Lecture 2  Sorting

Expected Partition

partition. balanced ain iswhich

,2

1)110(

1

is subtreeeach of size

expected theThus, ./1y probabilit thehas

positionlast in the appearsnumber each then

,considered are nspermutatio possible all If

n

nn

n

Page 72: Lecture 2  Sorting

Expected Running Time

1

11

1

1

1

1

lg2

))]0([E)]1([E(1

))]2([E)]1([E(1

))]1([E)]0([E(1

)](E[

:Induction

log)](E[

Quicksort of timerunning expected the:Guess

partition of timerunning the:Assume

n

i

iin

cnc

ncTnTn

ncnTTn

ncnTTn

nT

ncnnT

nc

Page 73: Lecture 2  Sorting

).)2/3lg(/ Choose(

))2/3lg((lg 3

2lg

3

12lg)1(

2/)1(

)1(21lg)1(

lg)1(

lg2

1

11

1

222

1

))1(/(21

11

1

11

cc

nccncnn

cnc

nncnc

nn

nncnc

incnc

iin

cnc

nnn

i

i

n

i

Page 74: Lecture 2  Sorting

Randomized Quicksort

);,,(Partition

];[][ exchange

);,(Random

),,(Partition-Randomized

rpA

rAiA

rpi

rpA

Page 75: Lecture 2  Sorting

Randomized Quicksort

);,1,(Quichsort-Ramdomized

);1,,(Quichsort-Ramdomized

);,,(Partition-Randomized

),,Quicksort(-Randomized

rqA

qpA

rpAq

rpA

Page 76: Lecture 2  Sorting

What we learnt in this lecture?

• What is heap, max-heap and min-heap?

• Heapsort and Quicksort.

• What is expected running time?