lecture4

# lecture4 - Lecture 4 Divide and Conquer for Nearest...

This preview shows pages 1–7. Sign up to view the full content.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Lecture 4 Divide and Conquer for Nearest Neighbor Problem Shang-Hua Teng Merge-Sort( A,p,r ) A procedure sorts the elements in the sub-array A [ p..r ] using divide and conquer • Merge-Sort( A,p,r ) – if p >= r , do nothing – if p< r then • Merge-Sort( A,p,q ) • Merge-Sort( A,q+1,r ) • Merge( A,p,q,r ) • Starting by calling Merge-Sort( A,1,n ) 2 / ) ( r p q + ← A = MergeArray( L,R ) Assume L [ 1:s ] and R[ 1:t ] are two sorted arrays of elements: Merge-Array( L,R ) forms a single sorted array A [ 1:s+t ] of all elements in L and R . • A = MergeArray( L,R ) – – – for k 1 to s + t • do if – then – else 1 ]; [ ] [ + ← ← i i i L k A 1 ]; [ ] [ + ← ← j j j R k A ∞ ← + ∞ ← + ] 1 [ ; ] 1 [ t R s L ] [ ] [ j R i L ≤ 1 ; 1 ← ← j i Complexity of MergeArray • At each iteration, we perform 1 comparison, 1 assignment (copy one element to A ) and 2 increments (to k and i or j ) • So number of operations per iteration is 4. • Thus, Merge-Array takes at most 4(s+t) time. • Linear in the size of the input . Merge ( A,p,q,r ) Assume A [ p..q ] and A[ q+1..r ] are two sorted Merge( A,p,q,r ) forms a single sorted array A [ p..r ]. • Merge (A,p,q,r) – – – – ∞ ← + ∞ ← + ] 1 [ ; ] 1 [ t R s L ; ; 1 q r t p q s- ← +- ← ] , 1 [ ]; .. [ r q A R q p A L + ← ← ) , ( ] .. [ R L MergeArray r p A ← Merge-Sort( A,p,r ) A procedure sorts the elements in the sub-array A [ p..r ] using divide and conquer...
View Full Document

## This note was uploaded on 03/08/2010 for the course CS 232 taught by Professor Bera during the Spring '09 term at BU.

### Page1 / 32

lecture4 - Lecture 4 Divide and Conquer for Nearest...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online