{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Notes 8 - Week 8 Searching Chapters 14 and 15 will not be...

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

View Full Document Right Arrow Icon
Week 8: Searching Chapters 14 and 15 will not be covered. Chapter 16 reviews several searching methods including the following: 1. arrays unsorted sequential iterative 2. arrays unsorted sequential recursive 3. arrays sorted sequential iterative (homework exercise) 4. arrays sorted sequential recursive (exercise) 5. arrays sorted binary iterative (homework exercise) 6. arrays sorted binary recursive 7. linked unsorted sequential iterative 8. linked unsorted sequential recursive 9. linked sorted sequential iterative 10. linked sorted sequential recursive 1. Iterative sequential search of unsorted array: First consider the following poor algorithm for searching: //Algorithm 1 boolean found; for (int i=0; i<lenght; i++) { if (searchValue.equals(entry[i]) ) found = true; else found = false; } return found; Question : What does Algorithm 1 return? Answer : Whether the very last element is equal to the search value (after doing a lot of unnecessary work first!) The following algorithm will work but keeps looping unnecessarily! //Algorithm 2 boolean found=false; for (int i=0; i<lenght; i++) { if (searchValue.equals(entry[i]) ) found = true; } return found; To make this more efficient either return true, instead of setting found to true or: //Algorithm 3 boolean found=false; for (int i=0; i<lenght && !found; i++) { if (searchValue.equals(entry[i]) ) found = true; } return found; The following example is similar to the one in the text on page 419 figure 16-2.
Background image of page 1

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

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

{[ snackBarMessage ]}