{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Algo1Notes

# Algo1Notes - 1 Here is a simple algorithm written in...

This preview shows pages 1–8. Sign up to view the full content.

1

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Here is a simple algorithm written in pseudocode. Remember, pseudocode is a “language” that allows designing and analyzing algorithms, but that is short of a “programming language”. Computers cannot execute pseudocode directly. Pseudocode is the first of the 2 step approach for designing and implementing algorithms. Notice the main parts of the pseudocode specification of the algorithm: input, output, and actual algorithm consisting of step-by-step instructions. The name of the algorithm is Minimum, and it has two (input) parameters: an array of integers called A, and the length of the array (i.e. the number of elements) called n. 2
In order to improve algorithm (and code) readability, the algorithm designer (i.e. you) adds comments. Comments are intended to clarify, explain, and are not part of the algorithm instructions. Comments are not executed, they are not instructions. 3

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
As discussed in the previous lecture, the algorithms are written to be as general as possible, in order to be useful in as many circumstances as possible. The algorithm parameters stand for an infinite number of input combinations: arrays with 10, 1000, 10000 elements, with elements with large or small values, etc. 4
This particular algorithm finds the minimum in an array of numbers (integer numbers). The output is the minimum over all elements of A, which is called Min, and it is “returned” to the caller of the minimum algorithm. The caller stands here generically for whomever wanted to know the minimum of the array A of length n. This could be a user, and the minimum value is returned through a printout on the screen, or some other algorithm, case in which the minimum algorithm is used as a sub-algorithm. The strategy is to initialize the minimum (stored in variable Min) to be the first element in the array, and then to traverse the remaining part of the array (i.e. elements index 1 to n-1) to see if there is a smaller element than the current minimum. If such an element is found, Min is updated. Min stores the current minimum, and has the true or final minimum only at the end, because it could be that the last element in the array is the actual minimum. Notice how the array is traversed with a for loop. The for loop iterates over the array index i. The first value for i is 1, since we do not need to test against the first element of the array. The decision whether to update Min is done with an if statement. The assignment 5

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Min = A[i] is only executed when A[i] is less than the current minimum. There is no else branch for the if. In other words, if the condition A[i] < Min is not true (i.e. it is false, or A[i] >= Min), nothing happens, no instruction needs to be executed. 5
Notice that indentation and the endif and endfor statements help structure the pseudocode. The if-endif statement is “inside the for loop”, meaning that the if statement is executed at every iteration.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}