# Lecture19 - LinearSearch...

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

Computer Science I - Martin Hardwick Lecture 1 -- 1 Linear Search bool banking::IsPresent (int num) const // Searches the list for num,  // reporting whether it was found { int  k; // loop variable //search for the num in the list for (k = 0; k < bank.size(); k++) { if (bank[k].get_num() == num) { return true; } } // didn't find num return false; } Here is a search algorithm for the Bank class. it is called  linear search It works for a list in any order. How much work does it do to find a number in a list? don’t find -->  look at all  items in the list find (worst case) --> look at  all items in the list find (best case) --> look at  one item in the list find (average case) --> look at  half the items in the list

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

View Full Document
Computer Science I - Martin Hardwick Lecture 1 -- 2 “Big O”Notation Need a measure of the amount of work required by an algorithm relative to the size of the input. allows comparison of different algorithms to solve the same  problem make it relative to the input size because you would expect all  algorithms to do more work when given more input The linear search algorithm is said to be an O(n) algorithm for both the average case and the worst case, where n is the length of the list to search. O( n ) is read “order  n this means that the amount of work done by the algorithm is  proportional to  c n  where  c  is a constant we don’t necessarily know what  c  is, but we know that it is a  constant c  is the overhead of searching one number in the list
Computer Science I - Martin Hardwick Lecture 1 -- 3 Binary Search Algorithm The list must be ordered for binary search to work.

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.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern