section 5 - algorithms Flashcards
the beginning and the end of the algorithm are put in boxes with rounded corners - they are sometimes called terminals
anything that’s put into or taken out of the algorithm goes into a parallelogram box
general instructions processes and calculations go into rectangular boxes
decisions often yes and no questions are put into diamond boxes
sub programmes reference other flow charts
arrows connect boxes and show the direction you should follow some boxes might have multiple Arrows coming in or going out of them
flowcharts
can show sequences, selections, iterations or a combination of them
sequence flowchart to calculate salary of worker after 10% pay increase
selections flowchart to check if a password is valid - has more than six characters & is different from the username
iterations flowchart - linear search
binary search
1) Find the middle item in the ordered list - in a list of n items do (n + 1) ÷ 2
and round up if necessary to find middle item
2) If this is the item you’re looking for, then stop the search you’ve found it.
3) If not, compare the item you’re looking for to the middle item. If it comes before the middle item, get rid of the second half of the list. If it comes after the middle item, get rid of the first half of the list.
4) You’ll be left with a list that is half the size of the original list. Repeat steps 1) - 3) on this smaller list to get an even smaller one. Keep going until you find the item you’re looking for.
binary search example
Use the binary search algorithm to find the number 99 in the following list.
7, 21, 52, 59, 68, 92, 94, 99, 133
There are 9 items in the list so the middle item is the (9+1) ÷ 2 = 5th item. The 5th item is 68 and 68 < 99 so get rid of the first half of the list to leave:
92, 94, 99, 133
There are 4 items in the list so the middle item is the (4+1) ÷ 2 = 3th item (rounded up). The 3th item is 99
Linear Search
1) Look at the first item in the unordered list.
2) If this is the item you’re looking for, then stop the search - you’ve found it.
3) If not, then look at the next item in the list.
4) Repeat steps 2) - 3) until you find the item that you’re looking for or you’ve checked every item
linear search example
7, 21, 52, 9, 68, 92, 94, 99, 133
linear vs binary search
A linear search is much simpler than a binary search but not as efficient. A linear search can be used on any type of list, it doesn’t have to be ordered. Due to it being inefficient, a linear search is often only used on small lists.
Once the list has been ordered, a binary search is much more efficient than a linear search. In general a binary search takes fewer steps to find the item you’re looking for, which makes it more suitable for large lists of items.