1. Trang chủ
  2. » Giáo án - Bài giảng

23 1 partitionsubroutine tủ tài liệu training pdf

10 32 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 10
Dung lượng 2,14 MB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

The Par00on Subrou0ne Design and Analysis of Algorithms I... -‐Pick element of array -‐Rearrange array so that -‐LeE of pivot => less than pivot -‐Right of pivot => greater than pivot No

Trang 1

The Par00on Subrou0ne

Design and Analysis

of Algorithms I

Trang 2

PAR00ONING AROUND A PIVOT

Key Idea : par00on array around a pivot element

-‐Pick element of array

-‐Rearrange array so that

-‐LeE of pivot => less than pivot

-‐Right of pivot => greater than pivot

Note : puts pivot in its “righJ ul posi0on”

pivot

> pivot

< pivot

Trang 3

TWO COOL FACTS ABOUT PAR00ON

memory [see next video]

2 Reduces problem size

Trang 4

THE EASY WAY OUT

Nextcore AI -Gopal Shangari

Note : Using O(nti extra memory, easy to par00on around

pivot in O(nti 0me.

pivot

Trang 5

Nextcore AI -Gopal Shangari

Assume : pivot = 1st element of array

[ if not, swap pivot <-‐-‐> 1st element as preprocessing step ]

High – Level Idea :

Already par00oned unpar00oned

-‐Single scan through array

-‐ invariant : everything looked at so far is par00oned

Trang 6

Nextcore AI -Gopal Shangari

unpar00oned

unpar00oned

unpar00oned

Swap

Trang 7

PAR00ON EXAMPLE (CON’DTI

Nextcore AI -Gopal Shangari

par00oned par00oned unpar00oned unpar00oned

Fast forwarding

Trang 8

Nextcore AI -Gopal Shangari

Par00on (A,l,rti

-‐p:= A[l]

-‐i:= l+1

-‐for j=l+1 to r

-‐if A[j] < p

[ input corresponds to A[l…r]]

[if A[j] > p, do nothing ]

-‐swap A[j] and A[i]

-‐i:= i+1

Trang 9

RUNNING TIME

Nextcore AI -Gopal Shangari

Running 0me = O(nti, where n = r – l + 1 is the length of the

input (subti array.

Reason : O(1ti work per array entry.

Also : clearly works in place (repeated swapsti

Trang 10

Claim : the for loop maintains the invariants :

Nextcore AI -Gopal Shangari

1 A[l+1], ,A[i-‐1] are all

less than the pivot

2 A[i],…,A[j-‐1] are all greater than pivot

[ Exercise : check this, by induc0on ]

Consequence : at end of for loop, have:

=> aEer final swap, array par00oned

around pivot

Ngày đăng: 17/11/2019, 07:34