You can print out this web page, fill it out, and hand it in or download the .rtf version, fill it in on the computer, and submit it via D2L. The .rtf version is located at sortLab.rtf
Create an array and initialize it with the numbers 12, 9, 13, 5, and 10. You will now play computer. You must evaluate each statement and supply the values for the various variables, make the decisions and do any processing that might be necessary. I have supplied the code below for the bubble sort which you will "walk" through while you are doing the same processing that your computer would do.
We will start with just the for loop.
1. for (int i = 0; i < SIZE -1; i++) { 2. if (array[i] > array[i+1]) { 3. //switch the elements' (numbers) order in the array 4. hold = array[i]; 5. array[i] = array[i+1]; 6. array[i+1] = hold; 7. swapped = true; 8. } 9. }
Using the for loop as given above, and the value you have stored in your array, read the code line by line and fill in the current value for each variable as the code progresses. We are working with five numbers, so the for loop will run 4 times. A table is provided for each iteration of the loop.
Current values in the array at the start:
Index 0 | Index 1 | Index 2 | Index 3 | Index 4 |
---|---|---|---|---|
Fill in the table with the correct values as you walk through the code. array[i] > array[i+1] will evaluate to true or false. Just put a line through the cells for lines 7, 8, and 9 if the if evaluates to false.
Line Number | 1 | 2 | 2 | 2 | 4 | 5 | 6 | 7 |
---|---|---|---|---|---|---|---|---|
Variable | i | array[i] | array[i+1] | array[i]>array[i+1] | hold | array[i] | array[i+1] | swapped |
Value |
Current values in the array after iteration 1:
Index 0 | Index 1 | Index 2 | Index 3 | Index 4 |
---|---|---|---|---|
Line Number | 1 | 2 | 2 | 2 | 4 | 5 | 6 | 7 |
---|---|---|---|---|---|---|---|---|
Variable | i | array[i] | array[i+1] | array[i]>array[i+1] | hold | array[i] | array[i+1] | swapped |
Value |
Current values in the array after iteration 2:
Index 0 | Index 1 | Index 2 | Index 3 | Index 4 |
---|---|---|---|---|
Line Number | 1 | 2 | 2 | 2 | 4 | 5 | 6 | 7 |
---|---|---|---|---|---|---|---|---|
Variable | i | array[i] | array[i+1] | array[i]>array[i+1] | hold | array[i] | array[i+1] | swapped |
Value |
Current values in the array after iteration 3:
Index 0 | Index 1 | Index 2 | Index 3 | Index 4 |
---|---|---|---|---|
Line Number | 1 | 2 | 2 | 2 | 4 | 5 | 6 | 7 |
---|---|---|---|---|---|---|---|---|
Variable | i | array[i] | array[i+1] | array[i]>array[i+1] | hold | array[i] | array[i+1] | swapped |
Value |
Current values in the array after iteration 4:
Index 0 | Index 1 | Index 2 | Index 3 | Index 4 |
---|---|---|---|---|
Now we will add the do...while loop. Fill in the values for the variables while evaluating the do...while loop. You have already done one repetition of the for loop. For the first run of the do while, put your beginning array values into the cells and the last values into the cells in the appropriate spaces in the chart below. You may not have to use all the runs of the do while loop that are provided.
1. do { 2. swapped = false; 3. for (int i = 0; i < SIZE -1; i++) { 4. if (array[i] > array[i+1]) 5. //switch the elements order in the array 6. hold = array[i]; 7. array[i] = array[i+1]; 8. array[i+1] = hold; 9. swapped = true; 10. } 11. } 12. } while(swapped == true);
Fill in the following tables with your values
First run of do while loop | |||||
do { | |||||
array values (before for) | |||||
swapped (before for) | |||||
for loop runs to completion | |||||
array values (after for) | |||||
swapped (after for) | |||||
} while (swapped == true); |
Second run of do while loop | |||||
do { | |||||
array values (before for) | |||||
swapped (before for) | |||||
for loop runs to completion | |||||
array values (after for) | |||||
swapped (after for) | |||||
} while (swapped == true); |
Third run of do while loop | |||||
do { | |||||
array values (before for) | |||||
swapped (before for) | |||||
for loop runs to completion | |||||
array values (after for) | |||||
swapped (after for) | |||||
} while (swapped == true); |
Fourth run of do while loop | |||||
do { | |||||
array values (before for) | |||||
swapped (before for) | |||||
for loop runs to completion | |||||
array values (after for) | |||||
swapped (after for) | |||||
} while (swapped == true); |
Fifth run of do while loop | |||||
do { | |||||
array values (before for) | |||||
swapped (before for) | |||||
for loop runs to completion | |||||
array values (after for) | |||||
swapped (after for) | |||||
} while (swapped == true); |
When does the do/while loop stop repeating?