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

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
1
Background image of page 1

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

View Full DocumentRight Arrow Icon
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
Background image of page 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
Background image of page 3

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

View Full DocumentRight Arrow Icon
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
Background image of page 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
Background image of page 5

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

View Full DocumentRight Arrow Icon
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
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/27/2012 for the course CS 177 taught by Professor Staff during the Spring '08 term at Purdue University-West Lafayette.

Page1 / 48

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

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online