# Algo1 - Basic algorithms with arrays 1 Finding minimum in...

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

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

View Full Document

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

View Full Document

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

View Full Document

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.

Unformatted text preview: Basic algorithms with arrays 1 Finding minimum in array of integers Input : A // array of integers n // number of elements in array (array size) Output : Min // value of element with smallest value Minimum (A, n) // name of algorithm and parameters Min = A[0] // initialize minimum as first element for i = 1 to n-1 // look at remaining elements if A[i] < Min then Min = A[i] endif endfor return Min endMinimum 2 Finding minimum in array of integers Input : A // array of integers n // number of elements in array (array size) Output : Min // value of element with smallest value Minimum (A, n) // name of algorithm and parameters Min = A[0] // initialize minimum as first element for i = 1 to n-1 // look at remaining elements if A[i] < Min then Min = A[i] endif endfor return Min endMinimum 3 // or # introduces comment what follows is not part of algorithm, it rather explains algorithm Finding minimum in array of integers Input : A // array of integers n // number of elements in array (array size) Output : Min // value of element with smallest value Minimum (A, n) // name of algorithm and parameters Min = A[0] // initialize minimum as first element for i = 1 to n-1 // look at remaining elements if A[i] < Min then Min = A[i] endif endfor return Min endMinimum 4 Algorithm is written for arrays of integers in general - Not for say an array with elements 0 or 1 - Not for say an array of length 10 - The size of the array is a variable, called n - The array is a variable called A - Its elements can have any value - A and n are input variables or parameters Finding minimum in array of integers Input : A // array of integers n // number of elements in array (array size) Output : Min // value of element with smallest value Minimum (A, n) // name of algorithm and parameters Min = A[0] // initialize minimum as first element for i = 1 to n-1 // look at remaining elements if A[i] < Min then Min = A[i] endif endfor return Min endMinimum 5 The algorithm iterates over array - Traverses array, checks all elements - First element doesnt need to be checked Minimum updated when smaller element found - Minimum is stored in a variable called Min Essential to initialize Min, or else first comparison to A[i] doesnt make sense Finding minimum in array of integers Input : A // array of integers n // number of elements in array (array size) Output : Min // value of element with smallest value Minimum (A, n) // name of algorithm and parameters Min = A[0] // initialize minimum as first element for i = 1 to n-1 // look at remaining elements if A[i] < Min then Min = A[i] endif endfor return Min endMinimum 6 endfor, endif, endMinimum make algorithm more readable - They reinforce indentation Finding minimum in array of integers Input : A // array of integers n // number of elements in array (array size) Output : Min // value of element with smallest value Minimum (A, n) // name of algorithm and parameters Min = A[0] // initialize minimum as first element for i = 1 to n-1 // look at remaining elements...
View Full 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 / 42

Algo1 - Basic algorithms with arrays 1 Finding minimum in...

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

View Full Document
Ask a homework question - tutors are online