2.3.1 Algorithms Flashcards
1
Q
What should you do before developing an algorithm?
A
- Algorithm must be well defined
- what inputs the algorithm will accept
- what outputs will it produce
- the scope of the algorithm
2
Q
What is execution time?
A
- How long an algorithm takes to run
3
Q
What ways can complexity be measured?
A
- Execution time - how many steps an algorithm will take with a given input
- Space - how much memory it takes up during execution
4
Q
What are the different Big O notation?
A
- O(1) - constant time
- O (Log2n) - logarithmic time
- O(n) - linear time
- O(n2) - squared time
- O(n3) cubed time
- O(nc) - polynomial time
- O(nn) - exponential time
5
Q
What is Bubble sort?
A
- They compare values and swaps if not in the correct order and does this with all pairs - process is repeated until all in correct order
6
Q
What are the features of pseudocode for bubble sort?
A
- Set swapped to false
- Add a for loop that loops to the end of the list
- IF list[n-1] > list[n] THEN
- Store the n-1 (greater one) in a temp variable and set n-1 to n
- Change n to temp and set swapped to true and repeat to next one
7
Q
What is the efficiency and space complexity of Bubble sort?
A
- Will perform n-1 comparisons every time
- Inefficient
- Simple algorithm and makes little additional use of memory other than a few variables - constant space complexity
8
Q
What is Insertion Sort?
A
- Assume the first item in the list is sorted
- This will start with the second item in the list and place it into a temporary variable. This is compared to the first item. If below all items are shifted by 1 (in sorted list)
9
Q
What is the space complexity of insertion sort?
A
- Makes very little additional use of memory other than few variables