cs lecture nov 25

# cs lecture nov 25 - int m=l(h-l/2 if(a[m]==value return...

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

cs lecture nov 25 Binary search -searching a sorted array int search( int a[], int n, in value); ^position if found, -1 if not found go to middle of array{ if value <a [m] search lower array if value >a[m] search upper array if value==a[m] return m; } eg. [-10][-7][0][2][11][14][30] probe = 2 (searching for zero) probe=-7 search [0] found! int search (int a[], int n, int value){ int i=0, h=n-1; while (h>=l){

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: int m=l +(h-l)/2; if(a[m]==value) return ,; else(if a[m]> value) h=m-1; else l=m+1; } return -1; }hz bookkeeping (low/high) ^low ^high idea: at each step value bust be in range of a[l] and a[h] [ ] or [ ] ^n, l n ^l ^n N time complexity suppose =2^k -1 o(k)=O(log n) if n=1000000 elements, about 20 probes if n=1,000,000,000, about 30 probes...
View Full Document

{[ snackBarMessage ]}

### Page1 / 2

cs lecture nov 25 - int m=l(h-l/2 if(a[m]==value return...

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

View Full Document
Ask a homework question - tutors are online