Question
To start off our additional exercises, we'll start with an extension of what we did as some of the required exercises, so if you did
To start off our additional exercises, we'll start with an extension of what we did as some of the required exercises, so if you did it the fancy way before, you should find this one straightforward.
In this exercise the goal is to implement the Bubble Sort algorithm in full.
As before the skeleton contains three methods:
-
A main method which will allow you to do your own testing, and give a working example.
-
A bubbleSort method where you'll do the actual implementation of the sorting algorithm.
-
A printArray method that's just there to help. Don't change it.
The bubbleSort method takes in an int[] called data. This is the array that you are to sort. This array may have any length. At the end of the method you should return data;. This line has already been added, so you can just leave it as is.
For the tests to ensure you are correctly implementing a Bubble Sort (and not some other sort, or using a library), you must print out the array using printArray(data) every time you swap elements in the array. You should not print it out any other time in the bubbleSort method.
import java.util.Arrays;
public class BubbleSort {
//Don't touch this method! private static void printArray(int[] a) { System.out.println(Arrays.toString(a)); }
//Complete this method. public static int[] bubbleSort(int[] data) {
//Implement a Bubble Sort on data here!
return data; }
//You can mess around in the main method //as you wish. As long as it compiles, //it won't affect the testing. public static void main(String[] args) {
int[] testData = {45, 23, 66, 12, 87, 19};
System.out.println("Sorting."); testData = bubbleSort(testData); System.out.println("After sorting the array is: "); printArray(testData); }
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started