cs lecture nov 25

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

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

View Full Document Right Arrow Icon
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){
Background image of page 1

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

View Full DocumentRight Arrow Icon
Background image of page 2
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]&gt; 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

Page1 / 2

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

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online