Description
In this lab you will write a class named ArrayUtils
that contains a
static method named bubbleSort
to perform the bubble sort algorithm on an array of numbers.
The algorithm is described in your textbook on p. 530, problem 4. Also read the section on sorting in Chapter 7 and make sure you understand the selection sort algorithm and code.
You must also write a separate static method that will display your array. Put this method
in your ArrayUtils
class. I recommend starting with this method to get familiar with using
arrays.
Before writing your bubble sort method you should try the algorithm on paper. Try the bubble sort algorithm on 6 3 8 4 7 (on paper) and make sure you understand the algorithm BEFORE you begin coding. Pay attention to how you determine if the array has been completely sorted. You will need to know how to determine if an array is sorted to write code for bubble sort. Hint: think about using a do-while loop and a boolean variable (this is not the only way to do it, but it might help to think about it).
You will also make a class named Lab8
for your main program. Your main program will
sort an array using your static bubbleSort
method in your ArrayUtils
class.
Your input array will be from the command line and you will have to read in the
arguments from the args array into your array to be sorted. Remember that args is
an array of Strings that is passed into your main
method. You will
need to convert the elements of this array into type int (use the Integer
wrapper class). To input arguments using the command line go to Run -> Run Configurations.
A Run Configurations window will appear. Click on the Arguments Tab at the top. Input your command-line
arguments into the area labeled "Program Arguments" and click the Run button.
You will need to swap elements in your array to perform the bubble sort algorithm.
Swapping elements is considered expensive and sorting algorithms are often evaluated
on the number of instructions required to perform the sort. To determine how "expensive"
bubble sort is for different arrays, your bubbleSort
method should count the number of
times a swap is performed. Your bubbleSort
method should return this number.
You can use your array display method for error checking while you are writing the program. Try using this method to check that you correctly put the arguments from args into your array. You can also use this method to display the array after each time you swap two numbers to make sure your algorithm is working correctly.
Write comments above all of your methods describing what they each do, what parameters they take if any, and what they return, if anything.
Your program should output the original array, the sorted array, and the number of swaps used to sort the array. Example output:
1 4 8 6 5 7 3 4 9
was sorted to
1 3 4 4 5 6 7 8 9
Total number of swaps: 13
Try your bubble sort code on these inputs:
1 3 5 7 9 2 4 6 8
9 8 7 6 5 4 3 2 1
1 3 4 6 2 7 5 9 8
Create yourlastname_lab8.txt that has your program output for each of these inputs.
How to turn in the assignment
ArrayUtils.java
and Lab8.java
source filesArrayUtils.class
and Lab8.class
class filesMANIFEST.MF
yourlastname_lab8.txt
, which has your program output for
the inputs specified in the assignment. Make sure to include the
standard writing assignment header.Grading
args
arrayYou can also lose points for incorrectly handing in the assignment, missing pledge headers, not turning in your printed code, not using appropriate white space, not using conventional names for any variables you declare, and not commenting your code where appropriate.