Description
In this lab you will write a class to perform the bubble sort algorithm. The algorithm is described on page 498 (problem 4) in your textbook. Before writing the class you should try the algorithm on paper.
Try the bubble sort algorithm on 3 6 8 4 7 (on paper) and make sure you understand the algorithm BEFORE you begin coding.
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 you will need to convert into type int (use the Integer wrapper class). To input arguments using the command line goto Build -> Run Arguments. A Run Arguments bar will appear at the top of your code window. Input your arguments in this bar and run your code as normal.
You probably want to write a method that will display your array. You can use this 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 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 you will add an instance variable to your class that will count the number of times a swap is performed. Once your array is sorted you should display your sorted array and the number of swaps used to sort the array.
Example output:
1 4 8 6 5 7 3 4 9Try your bubbleSort code on these inputs:
1 3 5 7 9 2 4 6 8Create yourlastname_lab7.txt that has your output for the inputs to bubble sort as well as your evaluation of the efficiency of bubble sort.
Grading
How to hand in the assignment