Lecture19 - LinearSearch boolbanking:IsPresent(intnum)const

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

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

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

View Full DocumentRight Arrow Icon
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
Background image of page 2
Computer Science I - Martin Hardwick Lecture 1 -- 3 Binary Search Algorithm The list must be ordered for binary search to work. Binary search is similar to the way that you look up a name in the
Background image of page 3

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

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 12

Lecture19 - LinearSearch boolbanking:IsPresent(intnum)const

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

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