3.1 Fundamentals of Algorithms Flashcards
1) What is an algorithm?
2) What is Algorithmic Thinking?
3) What is decomposition?
4) What is abstraction?
1) An algorithm is a sequence of steps that can be followed to complete a task.
2) Algorithmic thinking is the process of developing an algorithm to solve a problem.
3) Decomposition is the process of breaking problems down into smaller, more manageable parts.
4) Abstraction is the process of removing unnecessary detail from a problem.
1) What is the difference between an algorithm and a computer program?
2) What is a written description?
1) An algorithm describes the logic of a solution. A computer program is an implementation of this solution, written in a programming language.
2) A written description can be used to specify the steps of an algorithm.
What do the following do in flowcharts:
1) Arrows
2) Subroutines
3) Terminators
4) Inputs and Outputs
5) Process
6) Decision
1) Arrows are used to show the direction and flow of the program.
2) Subroutines are represented by a rectangle with two straight lines on either side.
3) Terminators are used for the start and end of subroutines or programs. It is represented by an oval.
4) An input or output is represented by a parallelogram.
5) A process is represented by a rectangle. It is used for an instruction or command.
6) A decision has two branches: True and False. It is represented by a diamond.
1) What is pseudocode?
2) What is a trace table?
3) What is a flow chart, and what can be used to signify the flow of the program.
1) Pseudocode is simplified language used to describe the sequence of steps used to solve a problem or complete a task. Pseudocode does not use program specific language.
2) A trace table allows you to formally record the state of variables, the outputs and the condition evaluations as you mentally execute the algorithm.
3) A flow chart can be used to visually represent an algorithm or program. Arrows can be used to signify the flow of the program.
1) What is a syntax error?
2) What is a runtime error?
3) What is a logic error?
1) Syntax errors are mistakes in the code, such as spelling and punctuation errors.
2) This is the error message you will receive if a problem occurs with your code while it is running.
3) They occur when the program runs without crashing, but produces an incorrect result. The error is caused by a mistake in the program’s logic. You won’t get an error message, because no syntax or runtime error has occurred.
Explain the linear search algorithm step by step
The instructions for performing a linear search can be written as:
1) Take a list of data and an item that is being searched for (the search item).
2) Repeat steps a–c, starting from the first item in the list, until you find the search item, or until the end of the list is reached:
a. Compare the item at the current position to the search item.
b. If the item at the current position is equal to the search item, stop searching.
c. Otherwise, go to the next item in the list.
Explain the binary search algorithm step by step
1) Take an ordered list of data and an item that is being searched for.
2) Maintain a range of items where the search item might be found.
Initially, set the range to be the entire list.
3) Repeat the following steps until you find the item you are searching for, or there are no more items to check (the range is empty):
a. Find the item in the middle of the range (the midpoint item).
b. Compare the midpoint item to the item you are searching for.
c. If the midpoint item is equal to the search item, stop searching.
d. Otherwise, if the midpoint item is less than than the search item, change the range to focus on the items after the midpoint.
e. Otherwise, if the midpoint item is greater than than the search item, change the range to focus on the items before the midpoint.
1) Explain what best-case scenario means
2) Explain what worst-case scenario means
1) The best-case scenario occurs when the item you are looking for results in the smallest possible number of steps.
2) The worst-case scenario takes place when the item you are looking for results in the greatest possible number of steps.
1) Explain what a linear search is
2) Explain what a binary search is
1) Linear search is an algorithm that involves checking each item in a list or sequence of data one at a time. When the data is unsorted, the only sensible option when searching for a particular item is to start at the beginning and look at every item until you find the one you want.
2) Start by setting the counter to the middle position in the list. If the value held there is a match, the search ends. Otherwise, the list is split in half depending on if the value is greater than or less than the midpoint.
1) What is the bubble sort algorithm?
2) What is the merge sort algorithm?
1) The bubble sort algorithm works by repeatedly going through a list, comparing adjacent items and swapping the items if they are in the wrong order.
2) Merge sort can be used to combine pairs of sorted lists repeatedly until all the items are in order.
Explain the bubble sort algorithm step by step
- Take a list of data to be sorted.
- Repeat step 1 (the pass) until no swaps are made:
Repeat steps a-c for all the items in the list, starting from the first one:
a. Compare the item at the current position to the one next to it.
b. If the item at the current position is greater than the one next to it, swap the items within the list.
c. Go to the next item in the list.
What is the merge sort algorithm?
- Take a list of data to be sorted.
- Repeatedly split the list in half until each item is in a list of its own.
- Repeat steps a to d until all lists have been merged.
a. Take two lists of data to be merged.
b. Create a new empty list for the merged items.
c. Repeat steps i to ii until one of the lists of items is empty:
i. Compare the first items of the two lists.
ii. Place the item that is lower into the merged list in the next available position.
d. Then place each item from the remaining list into the merged list in order.
What are the advantages and disadvantages of linear and binary search?
Advantages of linear search:
- Small lists can be searched quite quickly
- Easier to implement
- The data does not need to be in order
Advantages of binary search:
- Much faster at searching large lists than linear search
What are the advantages and disadvantages of merge sort and bubble sort
Advantages of bubble sort:
- Simplest and easiest to code
- Uses less memory
Disadvantages:
- Not suitable with larger lists
- Inefficient and slow
Advantages of merge sort:
- Far more efficient and fast
- Suitable for a large number of items
-Disadvantages:
- Uses more memory
- More complex to program
1) How do you assign a variable and a constant in pseudocode?
2) How do you make a comment in pseudocode?
3) How do you do integer division and modulus operator in pseudocode?
4) How do you show boolean operators?
1) Variable name ← Value, or CONSTANT Variable name ← Value for a constant
2) # Comment
3) Integer division: 9 DIV 5; Modulus operator: 9 MOD 5
4) AND, OR, NOT, eg. (3 = 3) AND (3 ≤ 4), NOT (a < b)