3.1 Fundamentals Of Algorithms Flashcards
Algorithm definition:
= sequence of steps followed to complete a task
Decomposition definition:
= breaking complex problem down into smaller sub-problems & solving each one individually
Abstraction definition:
= removing unnecessary detail from a problem (picking out important bits)
Two ways to write an algorithm:
- pseudo-code
- flowcharts
Pseudo-code purpose & advantages:
= clearly shows an algorithms steps without the finer details
- quick to write
- easy to understand
- easily converted into any programming language
Pseudo-code signs:
Assigning a variable: _
Flowchart commands
Oval = start/stop
Parallelogram = outputs/inputs
Rectangle = processes (instructions/calculations)
Diamond = decision
Rectangle with added vertical lines = subroutine
Arrow = shows direction
Types of flowcharts
- sequences (Straight from start to end)
- iteration (Contains a loop, allows tasks to be repeated)
- selections (Multiple ways from start to stop, includes decision)
Types of search algorithms:
= to find items in a list
- binary search
- linear search
Binary search
1) find middle item in ordered list
2) if this is the item you’re looking for, stop
3) if not, compare middle term with desired item (if it comes before the middle term, get rid of 2nd half of list, if it comes after, get rid of 1st half)
4) repeat steps 1-3 until the desired item is found
(Divide-and-conquer algorithm)
Advantages & disadvantages of binary search:
+
Faster/more efficient on large datasets
-
Dataset must be sorted(ordered) before
Linear search:
1) check first item in dataset
2) if this is the item you are looking for, stop
3) if not look a next item in list
4) repeat steps 2-3 until desired item found, or every item checked
Advantages & disadvantages of linear search
+
Very easy to implement (simple)
List doesn’t need to be sorted/ordered (used on any type of list)
- Slow/not efficient on long lists
Types of sort algorithms:
= sorts lists into numerical/alphabetical order
- bubble sort
- merge sort
Bubble sort:
1) look at first 2 items in list
2) if in right order, don’t do anything, if in wrong order, swap them
3) move onto next pair of items
4) repeat steps 2-3 until end of list = 1 pass
5) repeat steps 1-4 until no swaps in a pas (include last pass with 0 swaps)