Tino APCS

Lab 17.1 BubbleSort

Install Eclipse

Before installing Eclipse, you need to download and install the Java SE Development Kit (Java SE JDK) which is the version of Java that includes the compiler, Javadoc tool, etc. Get the JDK 8 here.

After installing the JDK, you need to download and install the proper version of Eclipse. On the Eclipse packages downloads page, scroll down a bit and get the version that says Eclipse IDE for Java Developers.

If you already use IntelliJ or NetBeans to their full capability that's fine but please stop using BlueJ after this week.

Create a new Eclipse Workspace for Semester 2

  1. Create a new Workspace and add a Lesson_17 project to it.

  2. Right-click the project and add a new Java Class to the project

    • Name the class PX_LastName_FirstName_BubbleSort
    • Use the default package (no package)
    • Check the box to automatically add a main method to your class

Create two bubble sort methods

  1. Add a public static void method named bubbleSort1 that takes in an array of ints and sorts the list into descending order by bubbling smallest elements to the right using i and i+1 as pairs.

    Test your method by creating several test arrays in the main method and then passing them into your bubble sort function. In order to verify that you are "bubbling" the right elements in the right direction, modify your method to print the array after each completion of the outer loop. This is how the autograder will know that your code has the correct intermediate steps and it's also helpful for you to see what's happening. Remember that you can print an array using the Arrays helper class which has to toString() method.

    Add this println statement at the end of each outer loop:

    for (int outer ...) {
        for (int inner ...) {
            ...
        }
        // Print at end of each outer loop
        System.out.println(Arrays.toString(myArray));
    }
    

    Here is an example of output with the printing after each iteration of the outer loop:

    Starting array: [5, 7, 2, 4, 3, 9]   (printed in main method)
    [7, 5, 4, 3, 9, 2]                (printed in bubbleSort method)
    [7, 5, 4, 9, 3, 2]                (printed in bubbleSort method)
    [7, 5, 9, 4, 3, 2]                (printed in bubbleSort method)
    [7, 9, 5, 4, 3, 2]                (printed in bubbleSort method)
    [9, 7, 5, 4, 3, 2]                (printed in bubbleSort method)
    
  2. Add a public static void method named bubbleSort2 that takes an array of Strings and sorts the list into ascending order by bubbling smallest elements to the left using i and i-1 as pairs. Print the array after each completion of the outer loop in the same way as you did for bubbleSort1.

    Remember that to compare Strings, you must use the compareTo() function to determine which String is "smaller."

Autograder warning

This lab will be graded by a cruel, unforgiving, merciless autograder, so be sure to follow the directions exactly, including naming your methods precisely as directed and using the correct access modifiers and other key words as required. If you print things you are not supposed to print or if you don't print the correct output, you will fail the autograder test cases, so it is also important to make sure you are doing the printing correctly.

On top of the autograder, it will also be further inspected by a human who will complain if your algorithm is executing unnecessary operations (i.e. your loop is performing more iterations than strictly required) or if you did not follow the directions exactly (i.e. you got the items in the right order, but didn't use the correct pairs for comparison). Make sure your code meets all requirements and performs exactly the operations required to guarantee the job is done - no more and no less. Think carefully and be concise. Don't just guess or pad your algorithm with extra steps "just in case".

Submit your PX_LastName_FirstName_BubbleSort code below

You must Sign In to submit to this assignment

Dark Mode

Outline