sort11 - Sorting Algorithms Continued Tracing Examples As...

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

View Full Document Right Arrow Icon
Tracing Examples As promised, this set of notes begins with a slightly different way of tracing code. These are the same examples as in the previous set of notes, but the tracing is maintained in a slightly different fashion. Example 1 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: Graphically, when the loop on line 6 terminates, the “memory” looks like: numbers array index 0 1 2 3 4 5 6 7 8 9 value 2 4 6 8 10 12 14 16 18 20 total i k Sorting Algorithms Continued - 1 Sorting Algorithms Continued ? ? ?
Background image of page 1

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

View Full DocumentRight Arrow Icon
All of total, i, and k are undefined at this point in the algorithm Statement 10 is a comment Statement 11: assigns total the value of 0 Statement 12 begins a for-loop that ends at statement 15 Loop terminates. Statement 16 prints: The sum of your numbers is: 110. The final state of the memory looks like the following: Numbers array index 0 1 2 3 4 5 6 7 8 9 value 2 4 6 8 10 12 14 16 18 20 total i k i is undefined Example 2 Assume that we have an array named X which contains the values shown below. What are the values in every variable that appears in this algorithm after the loop has completed? Determine your answers by tracing the algorithm’s execution. Array X index 0 1 2 3 4 5 value 4 2 6 1 5 3 0 void main ( ) { 1 int a = 0, b = 0; Sorting Algorithms Continued - 2 110 ? 11
Background image of page 2
2 int i= 0, j= 5; 3 do 4 if (X[ i ] < X[ j ]){ 5 b = b + X[ j ]; 6 j = j – 1; 7 } 8 else { 9 a = a + X[ i ]; 10 i = i + 1; 11 } 12 while ( i <= j); 13 } a b i j The final values in the variables are: Array X remains unchanged a b i j Did you get these final values when you traced the algorithm’s execution? If you did, congratulations! you’ve successfully traced the execution of the algorithm. If you didn’t, try it again and this time be more careful! A common mistake when tracing algorithm execution is to use an array index as the value in the array rather than the value that is at that index position in the array. Perhaps this is where you made your mistake, so try it again. Mergesort Sorting Algorithms Continued - 3 0 4 6 12 13 0 3 8 0 1 2 3 4 5 4 3 13 8 4 3
Background image of page 3

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

View Full DocumentRight Arrow Icon
The mergesort sorting algorithm uses the divide and conquer strategy in which the original problem is split into two half-size, recursively solved problems. If the overhead of the base case was linear [O(N)] then the overall running time of the algorithm was O(N log 2 N). The mergesort is such an algorithm and is commonly
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

This document was uploaded on 06/12/2011.

Page1 / 11

sort11 - Sorting Algorithms Continued Tracing Examples As...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online