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

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

View Full Document Right Arrow Icon
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++)
Image of page 1

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

View Full Document Right Arrow Icon
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
Image of page 2
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.
Image of page 3

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

View Full Document Right Arrow Icon
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)
Image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    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.

    Student Picture

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

  • Left Quote Icon

    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.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    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.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern