Partition Example-Analysis of Algorithms-Lecture Slides, Slides of Design and Analysis of Algorithms

This lecture is part of lecture series for Design and Analysis of Algorithms course. This course was taught by Dr. Bhaskar Sanyal at Maulana Azad National Institute of Technology. It includes: Partition, Example, Pivot, Correct, position, Time, Space, Quick, Sort, Base, Case

Typology: Slides

2011/2012

Uploaded on 07/11/2012

dharmadaas
dharmadaas 🇮🇳

4.3

(55)

262 documents

1 / 25

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
51
Partition Example
A: 4 8 6 3 5 1 7 2
i=0
j=1
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19

Partial preview of the text

Download Partition Example-Analysis of Algorithms-Lecture Slides and more Slides Design and Analysis of Algorithms in PDF only on Docsity!

51

Partition Example

A:

i=

j=

52

Partition Example

A:

j=

i=

54

Partition Example

A:

i=

j=

i=

55

Partition Example

A:

i=

j=

57

Partition Example

A:

i=

j=

58

Partition Example

A:

i=

j=

60

Partition Example

A:

i=

j=

61

Partition Example

A:

i=

63

Partition(A, left, right)

1. x ← A[left]

2. i ← left

3. for j ← left+1 to right

4. if A[j] < x then

5. i ← i + 1

6. swap(A[i], A[j])

7. end if

8. end for j

9. swap(A[i], A[left])

10. return i

n = right – left +

Time: cn for some constant c

Space: constant

64

4 8 6 3 5 1 7 2

2 3 1 5 6 7 8

4

Quick-Sort(A, 0, 7)

Partition

A:

66

2

4 5 6 7 8

1

1

3

Quick-Sort(A, 0, 7)

Quick-Sort(A, 0, 0) , base case , return

67

2

4 5 6 7 8

1

33

Quick-Sort(A, 0, 7)

Quick-Sort(A, 1, 1) , base case

69

4 2

1 3

5 6 7 8 6 7 8 5

Quick-Sort(A, 0, 7)

Quick-Sort(A, 2, 2), return

Quick-Sort(A, 4, 7)

, partition

70

4

5

6 7 8

66 7 8

2

1 3

Quick-Sort(A, 0, 7)

Quick-Sort(A, 5, 7)

, partition