Bubble Sort
This exercise provides questions that will help you understand the Bubble Sort algorithm. The program below sorts the elements of an integer array in ascending order.
Download files: BubbleSort.java
Areas: Sorting algorithms.
|
|
BubbleSort.java |
|
View full source |
| Question 1 |
| What do lines 10 and 12-15 do? |
| Question 2 |
| Ignoring line 11, what does the inner loop do? |
| Question 3 |
| Assume the array list has the following elements: 8 9 6 1 2 4 5. What will the array arrangement be after the first iteration of the outer for loop? |
| Question 4 |
| What is the arrangement after the second iteration of the outer loop? |
| Question 5 |
| What is the purpose of the boolean variable sorted? |
| Question 6 |
| How do we change the code to sort the array in descending order? |
