COMP 110 Spring 2013

Lab 7

No submission required


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 Lab7 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. Make sure to select from the list on the left-hand side "Java Applications" the current project you are working on (in this case, e.g., Lab7). Then click on the Arguments Tab at the top. Input your command-line arguments into the area labeled "Program Arguments" and click the Apply button. From now on, Lab7 will run with those arguments. To clear or to change command-line inputs, simply repeat the same steps.

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