Questions
Use the debugger to find all bugs in the file BuggySearchAndSort.java and write a report.
1. The program with bugs
BuggySearchAndSort.java
1 | import java.util.Arrays; |
2. Report
2.1 Bug 1
2.1.1 The description of the bug
After running the BuggySearchAndSort.java, the code cannot stop, and the program’s output is as follows:
1 | The array is: 3 5 1 10 10 1 2 7 2 10 |
The first bug is that the element that needs to be checked is in the array, but the output shows that it is not. Based on the output above, I located the contains() method that caused the bug. This bug is a semantic error because the output is different from what we expected, and the program does not terminate abnormally.
2.1.2 The method to fix it
The code with bugs:
1 | public static boolean contains(int[] array, int val) { |
The above code will only judge whether the first number in the array is equal to the number to be queried. If the first number in the array is equal to the number to be queried, the program will return true. If not, it will return false. Therefore, the program does not check for numbers except for the first number in the array.
The code without bugs:
1 | public static boolean contains(int[] array, int val) { |
The fixed code is shown above. To fix this bug, remove the else clause of the if statement so that the code will check all the numbers in the array until it finds the one to query.
2.1.3 Steps to find the bug
According to the outputs, I navigated to line 29 of the code, added a breakpoint there, and clicked the debug button to debug. When the code is paused at line 29, I click the step into button to enter the contains() method, and then run the code step by step, and find that the code will jump out of the for loop after checking the first number of the array, not after checking all the numbers of the array. Therefore, removing the else branch of the for statement can fix the bug.
2.2 Bug 2
2.2.1 The description of the bug
The second bug is that the code cannot be stopped after running. According to the output, after printing the result of Arrays.sort(), there is no information output, so I suspect that the infinite loop is in the code after printing the result of Arrays.sort(). Therefore, I locate the bubbleSort() method in the main().
2.2.2 The method to fix it
The code with bugs:
1 | public static void bubbleSort(int[] array) { |
In the above code, there is a nested for loop. Because the variable j in the second for loop has not been updated, it becomes an infinite loop.
The code without bugs:
1 | public static void bubbleSort(int[] array) { |
To fix this bug, we can replace the variable i with j in the second for loop. Then after the value of j is greater than array.length - 1, the loop will end.
2.2.3 Steps to find the bug
First, according to the output, I located the place where the bubbleSort() method was called in the main() method and added a breakpoint there. Then I clicked the “step into” button to debug step by step and found that the second for loop cannot end, and the value of the updated variable j was not updated. The reason is that the updated variable is i instead of j. Therefore, replacing i++ with j++ can fix this bug.
2.3 Bug 3
2.3.1 The description of the bug
The third bug is that in the selectionSort() method, when using the for loop to traverse the array, array[i] is written as array[1], resulting in the output not being sorted in non-descending order.
2.3.2 The method to fix it
The code with bugs:
1 | public static void selectionSort(int[] array) { |
The code without bugs:
1 | public static void selectionSort(int[] array) { |
To fix this bug, we just need to replace array[1] in the for loop with array[i]. In this case, the program can traverse the array and find the index corresponding to the maximum value in the array.
2.3.3 Steps to find the bug
After fixing the first two bugs, the output shows that the selectionSort() is incorrect. So I located the selectionSort() method in the main() method and added a breakpoint there. Then I clicked the debug button and the step into button to debug step by step. In the for loop, it turned out that when looking for the index of the maximum value, array[i] is wrongly written as array[1]. Therefore, changing array[1] to array[i] can fix this bug.
2.4 Bug 4
2.4.1 The description of the bug
The fourth bug is that in the condition of the while loop, pos should be greater than or equal to 0, not just greater than 0. Also, when placing temp into last vacated position, the value of temp should be assigned to array[pos +1] instead of array[pos].
2.4.2 The method to fix it
The code with bugs:
1 | public static void insertionSort(int[] array) { |
The code without bugs:
1 | public static void insertionSort(int[] array) { |
To fix this bug, we need to replace pos > 0 in the for loop with pos >= 0, and use “array[pos] = temp” to replace “array[pos + 1] = temp”. In this case, the insertionSort() can sort an array into non-decreasing order.
2.4.3 Steps to find the bug
After fixing the first third bugs, the output shows that the insertionSort() is incorrect. So I located the insertionSort() method in the main() and added a breakpoint there. Then I clicked the debug button and the step into button to debug step by step. After some debugging, I found that in the condition of the while loop, the pos > 0 is incorrect, and it should be changed to pos >= 0. Besides, when placing temp into last vacated position, the statement array[pos] = temp is incorrect, and it should be changed to array[pos + 1] = temp.
3. The program without bugs
Codes: BuggySearchAndSort.java
1 | import java.util.Arrays; |
Outputs:
1 | The array is: 1 3 3 1 5 6 8 10 4 5 |