Question

In: Computer Science

GETTING STARTED Create an Eclipse Java project containing package “bubble”. Import the 3 starter files BubbleSorter.java,...

GETTING STARTED

Create an Eclipse Java project containing package “bubble”. Import the 3 starter files BubbleSorter.java, BubbleSortTestCaseMaker.java, and Statistician.java.

PART 1: Implementing BubbleSorter

Implement a very simple BubbleSorter class that records how many array visits and how many swaps are performed. Look at the starter file before reading on.

This class has an instance variable called “a”. Its type is int[]. This is the array that will be bubble-sorted in place. Usually a single letter is a bad variable name, especially for an instance variable. But in literature about sorting algorithms it’s common to use “a” for an array that will be sorted.

In the sort() method, the outer loop should execute n times, where n is the length of the array. The inner loop should compare all adjacent pairs of elements, starting with a[n-1] vs. a[n-2], and ending with a[1] vs. a[0]. If comparison determines that the elements should be swapped, the inner loop should do the swap and increment nSwaps. Whether or not the elements are swapped, the inner loop should increment nVisits by 2. Note that this isn’t the most efficient way to control the loops, but it’s simple so it’s ok for now.

Complete the isSorted() method, which you can later use to test your code. After you sort an array, analyze it with isSorted() to make sure it’s really sorted. Before testing, look at BubbleSortTestCaseMaker.java. It provides 4 methods that return test case arrays. The first 3 methods are obvious. The last method builds an array containing random ints; its contents will be different every time the method is called.

Test your BubbleSorter code in main(). The starter file sorts a tiny test array. When that works, replace getTiny() with getAlreadySorted(), then with getBackward(). For each case, record the number of visits and swaps; you’ll need them later.

When your first 3 test cases are correctly sorted, test several times with a larger input array, returned by BubbleSortTestCaseMaker.buildRandom(100, 1000). Caution: this method returns a different random array every time it is called. Large random test cases are good for increasing confidence that an algorithm is correct, but they are difficult to debug because of their size and because they aren’t repeatable. So it’s good practice to start with simple test cases that make debugging easy, then move on to bigger cases after you have fixed all the bugs you can find with simple cases. Run the test 3 times, and record the number of visits and swaps for both runs.

Make a table like the following, and enter the counts that you observed.

Test case

Number of visits

Number of swaps

Tiny

Already sorted

Backward

Random #1

Random #2

Random #3

PART 2: BubbleSorter complexity

Complete the Statistician class, and use it to do experiments on your bubble sorter to determine if its complexity is O(n2). You can't draw valid conclusions from a single observation, or from a small number of observations. You need to execute a statistically large number of times, so that your conclusions have statistical strength. For this lab, an experiment will be 1000 executions. Define a private final static int called N_REPETITIONS, whose value is 1000.

The getStats() method of the Statistician class has an arg for specifying an array length. In that method’s loop, a random array of the specified length is created. Then a BubbleSorter sorts the array. Replace the “Assert …” comment line with a line that asserts that the sorter has correctly sorted. Paste that line into your report. Remember to configure Eclipse to run with assertions enabled (Run -> Run Configurations, then “Arguments tab” and type “-ea” into VM Arguments). If an assertion error is ever thrown, go back and fix your sorter. After the assertion line, replace the next comment with lines that retrieve the number of visits and swaps from the sorter, and store those numbers in the appropriate array lists.

After the loop in getStats(), write code that analyzes the 2 array lists, and prints the minimum, average, and maximum observed number of visits and number of swaps. Think about the benefit of creating a helper method that computes and prints min/avg/max for any array list of longs. Did you use a helper method?

Before running the statistician, think about what you expect to see. You haven’t been told the complexity of the bubble sort algorithm, but for now suppose that for an input array of length n, both the number of visits and the number of swaps are O(n2). If sorting an array of length=1000 requires v visits and s swaps, approximately how many visits and swaps are required to sort an array of length=3000?

In main(), the starter code calls getStats() for array lengths 1000 and 3000. Run main(). If it takes more than 2-3 minutes, try with shorter array lengths (e.g. 500 and 1500); the second length should be 3x the first length. Paste your output into your report. Does the output support the hypothesis that bubble sort is O(n2)?

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

package bubble;

public class BubbleSorter
{
   private int[]       a;
   private long       nVisits;
   private long       nSwaps;
  
  
   public BubbleSorter(int[] a)
   {
       this.a = a;
   }
  
  
   public void sort()
   {
       for (/* control the outer loop */)
       {
           for (/* control the inner loop */)
           {
               // Increment number of visits by 2.
               if (/* if 2 elements you're visiting need to be swapped */)
               {
                   // Swap the elements and increment nSwaps.
               }
           }
       }
   }
  
  
   public String toString()
   {
       String s = nVisits + " visits, " + nSwaps + " swaps\n{";
       for (int n: a)
           s += " " + n;
       s += " }";
       return s;
   }
  
  
   public boolean isSorted()
   {
       /* Implement this */
   }
  
  
   public long getNVisits()
   {
       /* Implement this */
   }
  
  
   public long getNSwaps()
   {
       /* Implement this */
   }
  
  
   public int[] getArray()
   {
       return a;
   }
  
  
   public static void main(String[] args)
   {
       int[] a = BubbleSortTestCaseMaker.getTiny();
      
       BubbleSorter sorter = new BubbleSorter(a);
       sorter.sort();
       System.out.println(sorter);
       System.out.println(sorter.isSorted() ? "SORTED" : "NOT SORTED");
   }
}
---------------------------------------------------------------------------------------------------------

package bubble;

import java.util.*;


public class Statistician
{
   private static void getStats(int arrayLength)
   {
       ArrayList<Long> visitCounts = new ArrayList<>();
       ArrayList<Long> swapCounts = new ArrayList<>();
      
       for (int i=0; i<N_REPETITIONS; i++)
       {
           int[] array = BubbleSortTestCaseMaker.buildRandom(arrayLength, arrayLength*100);
           BubbleSorter sorter = new BubbleSorter(array);
           sorter.sort();
           // Assert that the sorter sorted correctly.
           // Append # visits and # swaps to the array lists.
       }

       // Compute and print min/average/max number of visits.
       // Compute and print min/average/max number of swaps.
   }
  
  
   public static void main(String[] args)
   {
       System.out.println("1000:");
       getStats(1000);
      
       System.out.println("3000:");
       getStats(3000);
   }
}
----------------------------------------------------------------------------------------------------------------------------------------

package bubble;

public class BubbleSortTestCaseMaker
{
   private final static int[]       TINY =
   {
       1000, 1, 2, 3
   };
  
   public static int[] getTiny()
   {
       return TINY;
   }
  
  
   private final static int[]       ALREADY_SORTED =
   {
       1, 2, 3, 4, 5, 6, 7, 8, 9, 10
   };
  
   public static int[] getAlreadySorted()
   {
       return ALREADY_SORTED;
   }
  
  
   private final static int[]       BACKWARD =
   {
       10, 9, 8, 7, 6, 5, 4, 3, 2, 1
   };
  
   public static int[] getBackward()
   {
       return BACKWARD;
   }
  
  
   public static int[] buildRandom(int length, int maxValue)
   {
       int[] array = new int[length];
       for (int i=0; i<length; i++)
           array[i] = (int)(Math.random()*(maxValue + 1));
       return array;
   }
  
  
   public static void main(String[] args)
   {
       for (int i: buildRandom(10, 100))
           System.out.println(i);
   }
}

Solutions

Expert Solution

Desired changes are made in below BubbleSorter class:

public class BubbleSorter
{
   private int[]       a;
   private long       nVisits;
   private long       nSwaps;
  
  
   public BubbleSorter(int[] a)
   {
       this.a = a;
   }
  
  
   public void sort()
   {   
       int n=a.length;

       for (int i=0;i<n;i++)
       {
           for (int j=n-1;j>=1;j--)
           {
               // Increment number of visits by 2.
                  nVisits+=2;

               if (a[j-1]>a[j])
               {   
                   //  a[j-1] and a[j] should be swapped
                   nSwaps++;
                   int temp=a[j];
                   a[j]=a[j-1];
                   a[j-1]=temp;
               }
           }
       }
   }
  
  
   public String toString()
   {
       String s = nVisits + " visits, " + nSwaps + " swaps\n{";
       for (int n: a)
           s += " " + n;
       s += " }";
       return s;
   }
  
  
   public boolean isSorted()
   {
       int n=a.length;
       boolean result=true;

       for(int i=1;i<n;i++)
       {
           if(a[i-1]>a[i])
           result=false;
       }
       
       return true;

   }
  
  
   public long getNVisits()
   {
       return nVisits;
   }
  
  
   public long getNSwaps()
   {
       return nSwaps;
   }
  
  
   public int[] getArray()
   {
       return a;
   }
  
  
   public static void main(String[] args)
   {
       // replace getTiny() with getAlreadySorted() or getBackward() or buildRandom(100, 1000) for different test array input

       int[] a = BubbleSortTestCaseMaker.getTiny();
      
       BubbleSorter sorter = new BubbleSorter(a);
       sorter.sort();
       System.out.println(sorter);
       System.out.println(sorter.isSorted() ? "SORTED" : "NOT SORTED");
   }
}

Output with getTiny() method:

Output with getAlreadySorted() method:

Output with getBackward() method:

Output with buildRandom(100, 1000) method:


Related Solutions

Using eclipse IDE, create a java project and call it applets. Create a package and call...
Using eclipse IDE, create a java project and call it applets. Create a package and call it multimedia. Download an image and save it in package folder. In the package, create a java applet where you load the image. Use the drawImage() method to display the image, scaled to different sizes. Create animation in Java using the image. In the same package folder, download a sound. Include the sound in your applets and make it repeated while the applet executes....
JAVA LANGUAGE Required Tasks: In Eclipse, create a Java Project as CS-176L-Assign5. Create 3 Java Classes...
JAVA LANGUAGE Required Tasks: In Eclipse, create a Java Project as CS-176L-Assign5. Create 3 Java Classes each in its own file Student.java, StudentList.java, and Assign5Test.java. Copy your code from Assignment 4 into the Student.java and StudentList.java Classes. Assign5Test.java should contain the main method. Modify StudentList.java to use an ArrayList instead of an array. You can find the basics of ArrayList here: https://www.w3schools.com/java/java_arraylist.asp In StudentList.java, create two new public methods: The addStudent method should have one parameter of type Student and...
JAVA LANGUAGE Required Tasks: In Eclipse, create a Java Project as CS-176L-Assign4. Create 3 Java Classes...
JAVA LANGUAGE Required Tasks: In Eclipse, create a Java Project as CS-176L-Assign4. Create 3 Java Classes each in its own file Student.java, StudentList.java, and Assign4Test.java. Copy your code from Assignment 3 into the Student.java and StudentList.java. Assign4Test.java should contain the main method. In Student.java, add a new private variable of type Integer called graduationYear. In Student.java, create a new public setter method setGraduationYear to set the graduationYear. The method will have one parameter of type Integer. The method will set...
Required Tasks: In Eclipse, create a Java Project as CS-176L-Assign5. Create 3 Java Classes each in...
Required Tasks: In Eclipse, create a Java Project as CS-176L-Assign5. Create 3 Java Classes each in its own file Student.java, StudentList.java, and Assign5Test.java. Copy your code from Assignment 4 into the Student.java and StudentList.java Classes. Assign5Test.java should contain the main method. Modify StudentList.java to use an ArrayList instead of an array. You can find the basics of ArrayList here: https://www.w3schools.com/java/java_arraylist.asp In StudentList.java, create two new public methods: The addStudent method should have one parameter of type Student and should add...
For this coding exercise, you need to create a new Java project in Eclipse and finish...
For this coding exercise, you need to create a new Java project in Eclipse and finish all the coding in Eclipse. Run and debug your Eclipse project to make sure it works. Then you can just copy and paste the java source code of each file from Eclipse into the answer area of the corresponding box below. The boxes will expand once you paste your code into them, so don’t worry about how it looks J All data members are...
Create a new Java Project named “Packages” from within Eclipse. Following the example in the Week...
Create a new Java Project named “Packages” from within Eclipse. Following the example in the Week 6 lecture, create six packages: Main, add, subtract, multiply, divide, and modulo. ALL of these packages should be within the “src” folder in the Eclipse package Explorer. ALL of the packages should be on the same “level” in the file hierarchy. In the “Main” package, create the “Lab04.java” file and add the needed boilerplate (“Hello, World!” style) code to create the main method for...
In a Package called characterArray, ********JAVA CODE********** 1) Create a char array containing your full name...
In a Package called characterArray, ********JAVA CODE********** 1) Create a char array containing your full name and print it out. 2) Create an uninitialized char array and copy into it      the char array containing your full name and print it out. 3) Create a Character array and copy into it the char array containing      your full name and print it out. 4) Into the Character array, use toUpperCase() to copy the char array containing     your full name...
In STS, create Java Project, called java_generics_yourNameLastname that; (Under source directory called, src, create a package...
In STS, create Java Project, called java_generics_yourNameLastname that; (Under source directory called, src, create a package csci3444.generics and put all code in it) Create a generic interface called “MyGenInterface” that takes 2 generic types K,V with below methods; public K getKey(); public V getValue(); Create a generic class called “MyGenClass” that implements “MyGenInterface” interface. has attributes “K key” and “V value” that can be inherited has a constructor that takes “K _key”, “V _value” inputs and initializes “key”, “value” attributes...
In JAVA (eclipse).... Extract both files and place them in your program’s folder. You will be...
In JAVA (eclipse).... Extract both files and place them in your program’s folder. You will be using them. In your driver class: • Create an array called “boyNames” and store all names from the BoyNames.txt file • Similarly, create an array called “girlsNames” and store all names from the GirlNames.txt file • Create a text menu that allowing users to: 1. Enter a name and the application must display a message indicating if the name is among the most popular...
Java: Complete the methods marked TODO. Will rate your answer! -------------------------------------------------------------------------------------------------- package search; import java.util.ArrayList; import...
Java: Complete the methods marked TODO. Will rate your answer! -------------------------------------------------------------------------------------------------- package search; import java.util.ArrayList; import java.util.List; /** * An abstraction over the idea of a search. * * @author liberato * * @param <T> : generic type. */ public abstract class Searcher<T> { protected final SearchProblem<T> searchProblem; protected final List<T> visited; protected List<T> solution; /**    * Instantiates a searcher.    *    * @param searchProblem    *            the search problem for which this searcher will find and   ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT