{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

# day13 - Searching and Sorting Introduction Before we go any...

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

Introduction Before we go any further into exploring algorithms that handle searching and sorting you need to be sure that you can trace the execution of an algorithm (code) in order to understand how the algorithm performs its operations. The first section of these notes deals with tracing code and showing a technique for doing it thoroughly. Tracing Code Review How do you determine if an algorithm is correctly solving the problem for which it was designed? How do you know that the decisions that algorithm makes are the correct ones? As an algorithm designer you must learn the art of code tracing. In other words, you must act just like a computer and execute the algorithm in the same step-by-step fashion by which the computer will execute the problem. To do this correctly, you cannot impose your knowledge of the problem or the design of the algorithm on the execution process. In other words, don’t try to see your way to the solution or skip some of the steps that the algorithm will make because you can tell what’s going to happen. This is a sure-fire way to develop an algorithm which will not be correct. In order to properly trace the execution of an algorithm you must act like a computer and on a sheet of paper draw a box for each variable which is declared in the algorithm. The set of boxes that you construct will play the role of the computer memory in your trace. As the values in the boxes change due to the execution of statements in the algorithm you are reflecting the changes of state that occur in the computer’s memory as the algorithm executes. Begin by executing the statements of the algorithm in exactly the same manner that the computer would execute them and maintaining the state of the memory by correcting changing the values of the variables in their corresponding boxes. At the end of the execution of the algorithm, the values in the boxes will reflect the final state of the memory and thus the final values in each of the variables. As the trace is performed all I/O operations are effected just as the computer would do. Thus, read statements will consume input values and make the corresponding assignments while print statements will produce the output of the algorithm as it executes. Below are the final set of examples in this set of notes which give examples of tracing the execution of algorithms. Example 1 Searching and Sorting - 1 Searching and Sorting

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

View Full Document
In the algorithms below the numbers preceding each line are for reference purposes only and are not part of the algorithm. 1 void main ( ) { 2 int numbers [10]; 3 int i, k, total; 4 //read in the numbers 5 for (i=0; i<=9; i++) 6 { 7 printf(“Enter your number:\n” ); 8 scanf(“%d”, &numbers[ i ]); 9 } 10 //sum the numbers 11 total = 0; 12 for (k=0; k <=9; k++) 13 { 14 total = total + numbers[ k ]; 15 } 16 printf(“The sum of your numbers is: %d .\n“, total); 17 } The trace Suppose the input is: 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 Statements 1 through 4 are either comments or defining variables Statement 5 begins a loop which ends at line 9 – which does the following: iteration line 5
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