# 3. Algorithms - MATH 224 Discrete Mathematics Algorithms...

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

1 08/27/09 MATH 224 – Discrete Mathematics Algorithms and Complexity An algorithm is a precise recipe or set of instruction for solving a problem. In addition, to be considered an algorithm the set of instructions must solve the problem with a finite number of steps. In other words, if the algorithm is implemented on a computer, it must terminate with a solution. An algorithm should leave nothing to chance and must not have any infinite loops. (There are some algorithms that do use random or probabilistic techniques, and therefore may leave some things to chance.) Algorithms solve problems such as sorting, find an object, finding the shortest path between two points, determining mathematical functions such as logarithms among others. Specifying an Algorithms Informal English description Formal English description Pseudo-Code Code in programming language (e.g., C++)

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

View Full Document
2 08/27/09 MATH 224 – Discrete Mathematics Describing an Algorithm – Sorting an Array 0. .n−1 Informal description of selection sort Find the smallest number and put it in the first position Find the second smallest number and put it in the second position Continue as above until the array is sorted Formal Description 1. Set i = 0 2. Find the smallest value between positions i and n −1 3. Swap the value at i with the smallest value 4. Set i = i +1 5. If i < n −1 go back to Step 2, otherwise quit
3 08/27/09 MATH 224 – Discrete Mathematics Describing an Algorithm – Sorting an Array 0. . n−1 Pseudo-Code void function sort(Array A[n]) for i in [ 0..n-2 ] pos := i for j in [ i+1 .. n-1 ] if A[j] < A [pos] pos := j fi rof swap( A [ i ], A[pos]) rof end sort C++ Version void function sort (A_type A[ ], int n) { for (int i = 0; i < n-1; i++) { pos = i; for (int j = i+1; j < n; j++) if (A[j] < A[pos]) pos = j; // fi // rof swap(A[i], A[pos]); } // rof } // end sort Note the use of “:=” for assignment as do Pascal and Ada.

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

View Full Document
4 08/27/09 MATH 224 – Discrete Mathematics Binary search of an Array 1. . n Pseudo-Code – Algorithm 3 from the textbook (Page 172)
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 08/26/2009 for the course MATH 224 taught by Professor Waxman during the Spring '08 term at Southern Illinois University Edwardsville.

### Page1 / 21

3. Algorithms - MATH 224 Discrete Mathematics Algorithms...

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

View Full Document
Ask a homework question - tutors are online