Lecture200 linear searsh

# Lecture200 linear searsh - Linear Search...

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

Lecture 1 -- 1 Computer Science I - Martin Hardwick 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 < data.size(); k++) { if (data[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
Lecture 1 -- 2 Computer Science I - Martin Hardwick “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
Lecture 1 -- 3 Computer Science I - Martin Hardwick 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

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 ]}

### Page1 / 12

Lecture200 linear searsh - Linear Search...

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

View Full Document
Ask a homework question - tutors are online