Download Algorithms: and more Lecture notes Algorithms and Programming in PDF only on Docsity! 15-105 Principles of Computation, Carnegie Mellon University - CORTINA 1 Algorithms: The recipe for computation Visualizing Algorithms with Flowcharts 2C 15-105 Principles of Computation, Carnegie Mellon University - CORTINA 2 Flowcharts Flowcharts are used to show the flow of control of an algorithm as it runs step by step in a visual manner. Flowcharts consist of the following components: An oval labeled START A sequence of boxes with algorithm operations Arrows that indicate the order that the boxes are evaluated in the algorithm A oval labeled STOP or END 15-105 Principles of Computation, Carnegie Mellon University - CORTINA 3 Sequential Flow An assignment operation sets a variable to a specific value or changes a variable to a new value. Represented in a flowchart using a rectangle with the assignment operation described inside Example: Set Z = X modulo Y 15-105 Principles of Computation, Carnegie Mellon University - CORTINA 4 Input An input operation sets a variable to a data value given by the user of the algorithm. Represented in a flowchart using a rhombus with a small arrow pointing in from the side The contents of the rhombus include the variable being initialized Example: Input x 15-105 Principles of Computation, Carnegie Mellon University - CORTINA 9 Loop Operations A loop operation indicates the sequence of operations that are to be repeated along with a condition to control the number of repetitions Represented in a flowchart using a diamond with a test condition inside Includes one or more flowchart operations with an arrow that flows back to an earlier point in the flow of the algorithm 15-105 Principles of Computation, Carnegie Mellon University - CORTINA 10 Loop Operations (cont’d) Example: y > 10 Add y to x Add 1 to y Set y = 1 Set x = 0 Output x yes no BODY OF THE LOOP 15-105 Principles of Computation, Carnegie Mellon University - CORTINA 11 Loop Operations (cont’d) Example: ? Add y to x Add 1 to y Set y = 1 Set x = 0 Output x no yes 15-105 Principles of Computation, Carnegie Mellon University - CORTINA 12 Loop Operations (cont’d) Example: Set p = 1 Do the following 10 times: Multiply p by 2 Output p Add an additional unused variable to control the number of times the loop repeats. i = 10 Multiply p by 2 Add 1 to i Set i = 0 Set p = 1 Output x yes no 15-105 Principles of Computation, Carnegie Mellon University - CORTINA 13 Bubble Sort (Original Algorithm) 1. Input n 2. Input a vector of values A[0], A[1], …, A[n-1] 3. Do the following n times: a. Let i = 0 b. Do the following n-1 times: i. If A[i] > A[i+1], exchange A[i] and A[i+1] ii. Add 1 to i 4. Output A 15-105 Principles of Computation, Carnegie Mellon University - CORTINA 14 Bubble Sort (Original Algorithm) 1. Input n 2. Input a vector of values A[0], A[1], …, A[n-1] 3. Let x = 0 4. Do the following while x is not equal to n: a. Let i = 0 b. Do the following n-1 times: i. If A[i] > A[i+1], exchange A[i] and A[i+1] ii. Add 1 to i c. Add 1 to x 5. Output A 15-105 Principles of Computation, Carnegie Mellon University - CORTINA 19 Subroutines An operation that is executed a number of times at different places in your algorithm can be extracted out and made into a subroutine. A call to a subroutine is represented by a rectangle with a wide arrow to its right A separate flowchart for the subroutine is written in the same way as the initial “main” flowchart that describes the algorithm. 15-105 Principles of Computation, Carnegie Mellon University - CORTINA 20 Bubble Sort (Original Algorithm) x = n Set i = 0 Set y = 0 Exchange A[i] and A[i+1] Set x = 0 Input n yes no Input A[0],…,A[n-1] Start y = n-1 yes no A[i] > A[i+1] yes no Output A Stop Add 1 to x Add 1 to y Add 1 to i Extract this out as a subchart. 15-105 Principles of Computation, Carnegie Mellon University - CORTINA 21 Bubble Sort (Original Algorithm) Main flowchart x = n Set x = 0 Input n yes no Input A[0],…,A[n-1] Start Output A Stop Add 1 to x Call BubbleUp 15-105 Principles of Computation, Carnegie Mellon University - CORTINA 22 Bubble Sort (Original Algorithm) Subchart Start Set y = 0 Exchange A[i] and A[i+1] y = n-1 yes no A[i] > A[i+1] yes no Add 1 to y Stop BubbleUp Set i = 0 Add 1 to i