# 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) procedure binary_search(x: integer a[1..n] : integer)
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern