Activity 1: Complete the given trace table using the following algorithm
snippet.
Algorithm:
1. Start
2. x = 2
3. total = 0
4. REPEAT Step 5 THREE TIMES
5. total = total + x
6. Print (total)
End
Trace Table:
| Line No. | x | total | output |
| 2 | 2 | | |
| 3 | 2 | 0 | |
| 5 (1st) | 2 | 2 | |
| 6 (1st) | 2 | 2 | 2 |
| 5 (2nd) | 2 | 4 | |
| 6 (2nd) | 2 | 4 | 4 |
| 5 (3rd) | 2 | 6 | |
| 6 (3rd) | 2 | 6 | 6 |
Final Output: 2, 4, 6
Activity 2: Insertion sort algorithm
Sort the following array using insertion sort:
10 4 5 8 6 9 2
Step-by-Step:
Start: [10, 4, 5, 8, 6, 9, 2]
Pass 1: [4, 10, 5, 8, 6, 9, 2]
Pass 2: [4, 5, 10, 8, 6, 9, 2]
Pass 3: [4, 5, 8, 10, 6, 9, 2]
Pass 4: [4, 5, 6, 8, 10, 9, 2]
Pass 5: [4, 5, 6, 8, 9, 10, 2]
Pass 6: [2, 4, 5, 6, 8, 9, 10]
Sorted Array: 2, 4, 5, 6, 8, 9, 10
Activity 3: Bubble sort algorithm
Sort the following array using bubble sort:
3 1 7 8 2 5 6 4 0
Step-by-Step (First Pass):
[1, 3, 7, 8, 2, 5, 6, 4, 0] (Swap 3,1)
[1, 3, 7, 8, 2, 5, 6, 4, 0] (No swap)
[1, 3, 7, 8, 2, 5, 6, 4, 0] (No swap)
[1, 3, 7, 2, 8, 5, 6, 4, 0] (Swap 8,2)
[1, 3, 7, 2, 5, 8, 6, 4, 0] (Swap 8,5)
[1, 3, 7, 2, 5, 6, 8, 4, 0] (Swap 8,6)
[1, 3, 7, 2, 5, 6, 4, 8, 0] (Swap 8,4)
[1, 3, 7, 2, 5, 6, 4, 0, 8] (Swap 8,0)
After First Pass: [1, 3, 7, 2, 5, 6, 4, 0, 8]
(The process continues until the array is fully sorted.)
Final Sorted Array: 0, 1, 2, 3, 4, 5, 6, 7, 8
Activity 4: Binary Search algorithm
Consider the following sorted array of seven elements and we need to find a number 9.
2 4 5 6 8 9 10
Step-by-Step:
Step 1: Middle element is 6 (index 3). 9 > 6, so search in the right half: [8, 9, 10].
Step 2: Middle element of right half is 9 (index 5). 9 == 9. Element found!
Activity 5: Linear Search algorithm
Consider the following array of eight elements and we need to find a number 9.
58 25 39 78 12 9 79 80
Step-by-Step:
Step 1: Compare 58 with 9. Not equal.
Step 2: Compare 25 with 9. Not equal.
Step 3: Compare 39 with 9. Not equal.
Step 4: Compare 78 with 9. Not equal.
Step 5: Compare 12 with 9. Not equal.
Step 6: Compare 9 with 9. Element found at index 5!