Every topic starts at New. Sign in and your practice counts.
The algorithm dry run means manual checking the execution of an algorithm by providing it specific set of inputs. This helps not only helps to verify algorithm's correctness but also to understand algorithm's behavior on certain inputs and conditions. By doing this, we are also able to identify logical errors, if any.
i. Take a variable max and initialize with the first element of the list.
ii. Iterate through all the remaining elements of the list.
iii.Compare each element with the variable max.
iv. If the compared element is greater than the variable max, update max variable with the compared element.
v. Now max variable contains largest number return it.
Example Input List: [3, 1, 4, 1, 5, 9, 2, 6]
max=3 (First element in the list)
$>> Compare 1 with 3: 1 is less than 3, so max will not change.
2>> Compare 4 with 3: 4 is greater than 3, update value of max to 4 .
Compare 1 with is less than 4 , so max will not change.
23> Compare 5 with 4: 5 is greater than 4, update value of max to 5 .
Compare 9 with is greater than 5 , update value of max to 9 .
Compare 2 with is less than 9 , so max will not change.
Compare 6 with is less than 9 , so max will not change.
2>> maxcontains 9, return this value.
If you visually see your input list, 9 is the largest number and our dry run of the algorithm have also correctly identified 9 as the maximum number. This shows that the algorithm steps are working as intended. The dry runs are the powerful tool that can be used to validate the algorithm logic before writing the code.
A trace table are also used to verify the algorithm correctness. While tracing the algorithm, the trace table records the values of variables and their respective change at each of the algorithm step. The final values of the variables are considered final output of the algorithm.
| Step | Current Element | Max (Initial) | Updated Max | Comment |
|---|---|---|---|---|
| 0 | 3 | 3 | 3 | Initialize max with first element |
| 1 | 1 | 3 | 3 | . so max remains 3 |
| 2 | 4 | 3 | 4 | 4 > 3. so update max to 4 |
| 3 | 1 | 4 | 4 | , so max remains 4 |
| 4 | 5 | 4 | 5 | , so update max to 5 |
| 5 | 9 | 5 | 9 | , so update max to 9 |
| 6 | 2 | 9 | 9 | , so max remains 9 |
| 7 | 6 | 9 | 9 | , so max remains 9 |
| End | - | - | 9 | Final result |
Table 2.2: Trace Table
In the above table at
This trace table shows how the algorithm processes each element of the list and how the max value is updated accordingly.