Java Code/No JOptionPane please 34. Code the Bubble Sort in a static method whos
ID: 3715942 • Letter: J
Question
Java Code/No JOptionPane please
34. Code the Bubble Sort in a static method whose parameter is an array of integers to be sorted. Provide a driver program to demonstrate that the method functions properly.
35. Modify the method described in the above exercise so that it counts and outputs the number of comparisons and swaps performed during each pass through the algorithm.
This a Bubble Sort that counts and outputs the number of comparisons and swaps performed during each pass through the algorithm. Be sure to provide the total number of comparisons and passes for the data set also. You should use the RandomIntegers class provided in the attachment to generate your data sets, There is a TestSort class that you should modify also to use the sort that you coded instead of the Arrays.sort method included there.
RandomIntegers Class:
import java.util.*;
public class RandomIntegers
{
private Random randomInteger;
private int testArray[];
public RandomIntegers(int n, int bound)
{
long seed = System.currentTimeMillis(); //Get current time as a long.
randomInteger = new Random(seed);//Use seed to generate random numbers.
testArray = new int[n];
for(int i = 0; i < n; i++)
testArray[i] = randomInteger.nextInt(bound);
}
public int[] getTestArray()
{
return testArray;
}
}
TestSort Class:
import java.util.*;
public class TestSort
{
public static void main(String args[])
{
//Create test array
int[] test = new int[20];
RandomIntegers rtest = new RandomIntegers(20, 100); // Generate 20 in the 0 to 100 range
test = rtest.getTestArray(); //get those integers
//Print it out
System.out.println("Unsorted integers: ");
printArray(test);
//Sort it - Students replace this with your sort (bubble, merge, or quick)
Arrays.sort(test);
//Print sorted array
System.out.println(" Sorted integers: ");
printArray(test);
}
public static void printArray(int a[])
{
for (int i = 0; i< a.length ; i++)
{
if(i % 5 == 0)
System.out.println( ); //keep it 5 to a line
System.out.print(a[i] + " ");
}
System.out.println();
}
}
Explanation / Answer
Included bubbleSort method in TestSort which print no of comparisons and no of swaps and does bubblesort:
TestSort.java
public class TestSort
{
public static void main(String args[])
{
// Create test array
int[] test = new int[20];
RandomIntegers rtest = new RandomIntegers(20, 100); // Generate 20 in
// the 0 to 100
// range
test = rtest.getTestArray(); // get those integers
// Print it out
System.out.println("Unsorted integers: ");
printArray(test);
// Sort it - Students replace this with your sort (bubble, merge, or
// quick)
bubbleSort(test);
// Print sorted array
System.out.println(" Sorted integers: ");
printArray(test);
}
public static void printArray(int a[])
{
for (int i = 0; i < a.length; i++)
{
if (i % 5 == 0)
System.out.println(); // keep it 5 to a line
System.out.print(a[i] + " ");
}
System.out.println();
}
// Bubble sorting
public static void bubbleSort(int[] unsorted) {
boolean swapped = true;
int j=1;
//to check number of comparisons and Swaps
int comparison=0, swaps=0;
while(swapped) {
swapped = false;
for(int i = 0; i < unsorted.length - 1; i++) {
comparison++;
if(unsorted[i] > unsorted[i + 1]) {
int temp = unsorted[i];
unsorted[i] = unsorted[i + 1];
unsorted[i + 1] = temp;
swapped = true;
swaps++;
}
}
}
System.out.println("Number of Comparisons: "+comparison);
System.out.println("Number ofSwaps: "+swaps);
}
}
RandomIntegers.java
import java.util.*;
public class RandomIntegers
{
private Random randomInteger;
private int testArray[];
public RandomIntegers(int n, int bound)
{
long seed = System.currentTimeMillis(); // Get current time as a long.
randomInteger = new Random(seed);// Use seed to generate random numbers.
testArray = new int[n];
for (int i = 0; i < n; i++)
testArray[i] = randomInteger.nextInt(bound);
}
public int[] getTestArray()
{
return testArray;
}
}
Sample Output:
Unsorted integers:
63 44 26 42 58
41 63 82 46 66
54 34 93 32 76
26 76 0 21 20
Number of Comparisons: 361
Number ofSwaps: 112
Sorted integers:
0 20 21 26 26
32 34 41 42 44
46 54 58 63 63
66 76 76 82 93
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.