CS110_09b_search - EECS110 9b Some Search Algorithms Jack...

Info iconThis preview shows page 1. Sign up to view the full content.

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

Unformatted text preview: EECS110: 9b Some Search Algorithms Jack Tumblin [email protected] . Search Name2, Name1 Cratchit, Bob Scrooge, Ebenezer Marner, Silas Fogg, Phineas Low, Sampson Searle, Aloysius Chuzlewit, Martin Salary Dep. Borough . . . 10.00 7 Dingley Dell 250.00 1 Strand 15.00 1 Durden 20.00 2 Devonshire 75.00 4 Hampstead 75.00 3 Bishopsgate 20.00 5 Fenchurch . . . And so on for about 250 entries . . . A master roster for employees Define a `workerT' data structure; holds all data for one worker, (HUGE: don't make more than you need!) HUGE Search staff[0] staff[1] staff[2] staff[3] staff[4] . . . Name2, Name1 Cratchit, Bob Scrooge, Ebenezer Marner, Silas Fogg, Phineas Low, Sampson Searle, Aloysius Chuzlewit, Martin Salary Dep. Borough . . . 10.00 7 Dingley Dell 250.00 1 Strand 15.00 1 Durden 20.00 2 Devonshire 75.00 4 Hampstead 75.00 3 Bishopsgate 20.00 5 Fenchurch . . . And so on for about 250 entries . . . Store it using an array of pointers-to-struct: workerT *pStaff[300]; // up to 300 entries Given string "Low, Sampson" how can we find salary? Linear Search Obvious answer: look through all the names until we find a match: int k,kmax; char *pKey; ... pKey = "Low, Sampson"; // look for this for(k=0; k<kmax; k++) // all k in staff[k] { if(NULL != staff[k]) //(skip empty ones) { if(strncmp(pStaff[k]->name, pKey, 80)==0) return(k); } } return(-1); // ERROR! Not found! Linear Search Obvious answer: look through all the names until we find a match: int k,kmax; char *pKey; ... pKey = "Low, Sampson"; // look for this for(k=0; k<kmax; k++) // all k in staff[k] { if(NULL != staff[k]) //(skip empty ones) { if(strncmp(pStaff[k]->name, pKey, 80)==0) return(k); } } return(-1); // ERROR! Not found! Linear Search Obvious answer: look through all the names until we find a match: int k,kmax; char *pKey; ... pKey = "Low, Sampson"; // look for this for(k=0; k<kmax; k++) // all k in staff[k] { if(NULL != staff[k]) //(skip empty ones) { if(strncmp(pStaff[k]->name, pKey, 80)==0) return(k); } } return(-1); // ERROR! Not found! Linear Search Not clever: To find 1 entry, we must test all N entries. To find N entries? Over and over again?!?! That's NxN = N2 cost! cost Common Sense: Sort First (by name (alphabetical), or by salary, by deductions, by years-of-service, or ...) Then Search much more efficiently Binary Search (of pre-sorted data) Core idea: Find middle of list, to split it into 2 halves. list Ba Ca Ce Fa Fe Fo Ga He Ho La Lo Mi Na Ne NuMo Pa Ph Qu Ra Where is Low, Sampson? Binary Search (of sorted data) Core idea: Find middle of list, to split it into 2 halves. list Did we find the answer? if yes, STOP. Ba Ca Ce Fa Fe Fo Ga He Ho La Lo Mi Na NeNuMo Pa Ph Qu Ra Where is Low, Sampson? Binary Search (of sorted data) Core idea: Find middle of list, to split it into 2 halves. list Did we find the answer? if yes, STOP Which half has the answer? Lo < La ? Lo > La ? Ba Ca Ce Fa Fe Fo Ga He Ho La Lo Mi Na NeNuMo Pa Ph Qu Ra Where is Low, Sampson? Binary Search (of sorted data) Core idea: Find middle of list, to split it into 2 halves. list Did we find the answer? if yes, STOP Which half has the answer? Lo < La ? Lo > La ? Ba Ca Ce Fa Fe Fo Ga He Ho La Lo Mi Na NeNuMo Pa Ph Qu Ra Where is Low, Sampson? Binary Search (of sorted data) Core idea: Find middle of list, to split it into 2 halves. list Did we find the answer? if yes, STOP Which half has the answer? Keep only that half. Repeat until answer is found . . . Ba Ca Ce Fa Fe Fo Ga He Ho La Lo Mi Na NeNuMo Pa Ph Qu Ra Where is Low, Sampson? Binary Search (of sorted data) Core idea: Find middle of list, to split it into 2 halves list Did we find the answer? if yes, STOP Which half has the answer? Keep only that half. Repeat until answer is found . . . Ba Ca Ce Fa Fe Fo Ga He Ho La Lo Mi Na NeNuMo Pa Ph Qu Ra Where is Low, Sampson? Binary Search (of sorted data) Core idea: Find middle of list, to split it into 2 halves list Did we find the answer? if yes, STOP Which half has the answer? Keep only that half. Repeat until answer is found . . . Lo = Nu ? Ba Ca Ce Fa Fe Fo Ga He Ho La Lo Mi Na NeNuMo Pa Ph Qu Ra Where is Low, Sampson? Binary Search (of sorted data) Core idea: Find middle of list, to split it into 2 halves list Did we find the answer? if yes, STOP Which half has the answer? Keep only that half. Repeat until answer is found . . . Lo < Nu ? Lo > Nu ? Ba Ca Ce Fa Fe Fo Ga He Ho La Lo Mi Na NeNuMo Pa Ph Qu Ra Where is Low, Sampson? Binary Search (of sorted data) Core idea: Find middle of list, to split it into 2 halves list Did we find the answer? if yes, STOP Which half has the answer? Keep only that half. Repeat until answer is found . . . Lo < Nu ? Lo > Nu ? Ba Ca Ce Fa Fe Fo Ga He Ho La Lo Mi Na NeNuMo Pa Ph Qu Ra Where is Low, Sampson? Binary Search (of sorted data) Core idea: Find middle of list, to split it into 2 halves list Did we find the answer? if yes, STOP Which half has the answer? Keep only that half. Repeat until answer is found . . . Ba Ca Ce Fa Fe Fo Ga He Ho La Lo Mi Na Ne NuMo Pa Ph Qu Ra Where is Low, Sampson? Binary Search (of sorted data) Implementation: could use array indices... Left-hand, right-hand array indices lh, rh define a list 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 . . . Ba Ca Ce Fa Fe Fo Ga He Ho La Lo Mi Na NeNuMo Pa Ph Qu Ra lh lh=0, rh=19 Where is Low, Sampson? rh Binary Search (of sorted data) Implementation: could use array indices... Find (integer) midpoint mid between lh & rh: staff[(lh+rh)/2] 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 . . . Ba Ca Ce Fa Fe Fo Ga He Ho La Lo Mi Na NeNuMo Pa Ph Qu Ra lh lh=0, mid mid=(0+19)/2=9, rh=19 rh Where is Low, Sampson? Binary Search (of sorted data) Implementation: could use array indices... If target == midpoint, report success!(NOT YET) else ... 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 . . . Ba Ca Ce Fa Fe Fo Ga He Ho La Lo Mi Na NeNuMo Pa Ph Qu Ra lh mid lh=0, mid=9, rh=19 Where is Low, Sampson? rh Binary Search (of sorted data) Implementation: could use array indices... If target < midpoint La (e.g. if target is Ca), Ca move rh just before midpoint, else ... 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 . . . Ba Ca Ce Fa Fe Fo Ga He Ho La Lo Mi Na NeNuMo Pa Ph Qu Ra lh rh mid rh lh=0, rh=8, mid=9 Where is Low, Sampson? Binary Search (of sorted data) Implementation: could use array indices... If target > midpoint La (e.g. if target is Ne), ( move lh just past midpoint. 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 . . . Ba Ca Ce Fa Fe Fo Ga He HoLa Lo Mi Na NeNuMo Pa Ph Qu Ra lh mid lh rh mid=9, lh=10, rh=19 Where is Low, Sampson? Binary Search (of sorted data) Implementation: could use array indices... REPEAT. Make new midpoint half-way between lh & rh 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 . . . Ba Ca Ce Fa Fe Fo Ga He HoLa Lo Mi Na NeNuMo Pa Ph Qu Ra Nu lh mid rh lh=10, mid=(9+19)/2 = 14, rh=19 Where is Low, Sampson? Binary Search (of sorted data) Implementation : could use array indices... If target == midpoint Nu, Nu report success!(NOT YET) else ... 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 . . . Ba Ca Ce Fa Fe Fo Ga He HoLa Lo Mi Na NeNuMo Pa Ph Qu Ra Nu lh mid rh lh=10, mid=14, rh=19 Where is Low, Sampson? Binary Search (of sorted data) Implementation: could use array indices... If target < midpoint Nu (e.g. if target is Na), Na move rh just before midpoint; 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 . . . Ba Ca Ce Fa Fe Fo Ga He HoLa Lo Mi Na NeNuMo Pa Ph Qu Ra Nu lh rh mid rh lh=10, rh=13, mid=14 Where is Low, Sampson? Binary Search (of sorted data) Implementation: could use array indices... REPEAT. Make new midpoint half-way between lh & rh 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 . . . Ba Ca Ce Fa Fe Fo Ga He HoLa Lo Mi Na NeNuMo Pa Ph Qu Ra Nu lh mid rh lh=10, mid=(10+13)/2 = 11, rh=13 Where is Low, Sampson? Binary Search (of sorted data) Implementation: could use array indices... If target == midpoint Mi, Mi report success!(NOT YET) else ... 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 . . . Ba Ca Ce Fa Fe Fo Ga He HoLa Lo Mi Na NeNuMo Pa Ph Qu Ra Nu lh mid rh lh=10, mid=11, rh=13 Where is Low, Sampson? Binary Search (of sorted data) Implementation: could use array indices... If target < midpoint Mi (e.g. if target is Lo), Lo move rh just before midpoint; 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 . . . Ba Ca Ce Fa Fe Fo Ga He HoLa Lo Mi Na NeNuMo Pa Ph Qu Ra Nu lh rh mid rh lh=10, rh=10, mid=11 Where is Low, Sampson? Binary Search (of sorted data) Implementation: could use array indices... REPEAT. Make new midpoint half-way between lh & rh 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 . . . Ba Ca Ce Fa Fe Fo Ga He HoLa Lo Mi Na NeNuMo Pa Ph Qu Ra Nu lh mid rh lh=10, mid=10, rh=10 Where is Low, Sampson? Binary Search (of sorted data) Implementation: could use array indices... If target == midpoint Lo, Lo report success! YES! 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 . . . Ba Ca Ce Fa Fe Fo Ga He HoLa Lo Mi Na NeNuMo Pa Ph Qu Ra Nu lh mid rh lh=10, mid=10, rh=10 Where is Low, Sampson? Binary Search (of sorted data) Implementation: Suppose NO target exists; What if target was Li instead? at this step we have: Li 0 1 2 3 < midpoint Lo; Lo move rh to just before midpoint; 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 . . . Ba Ca Ce Fa Fe Fo Ga He HoLa Lo Mi Na NeNuMo Pa Ph Qu Ra Nu lh rh mid rh rh=9, lh=10, mid=10 Where is Li, James? Binary Search (of sorted data) Implementation: if(rh < lh) then the target was not found! found 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 . . . Ba Ca Ce Fa Fe Fo Ga He Ho La Lo Mi Na NeNuMo Pa Ph Qu Ra lh rh if the target is Li (NOT IN THE LIST) then lh, rh pass over each other! lh Why Bother? HUGE speed improvement! Every cycle cuts list length in half; Cost for N = 2k length list is only k cycles For one search of a list of length N: Linear search cost: N Binary search cost: log2(N) EXAMPLE: search a 32-million-element list Linear search: 32 million cycles Binary search: 25 cycles ...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online