Docsity

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

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