Question
JAVA HELP - need to add to the BubbleSort class below, no changes to BubbleSortTest! // BubbleSort class import java.util.Random; public class BubbleSort { //
JAVA HELP - need to add to the BubbleSort class below, no changes to BubbleSortTest!
// BubbleSort class import java.util.Random;
public class BubbleSort { // class level variables // constructor public BubbleSort(int size) { // create and fill an array of specified size // with random ints in range 10-99
}
// sort elements of array with bubble sort public void sort() { // sort the array in ascending order in // accordance with the requirements of // exercise 19.5 and 19.6a & b.
} }
// helper method to swap values in two elements public void swap(int first, int second) { // swap elements }
// method to output values in array public String toString() { StringBuilder temporary = new StringBuilder();
// iterate through array for (int element : data) temporary.append(element + " ");
temporary.append(" "); // add endline character return temporary.toString(); } } // end class BubbleSort
//BubbleSortTest NO CHANGES
public class BubbleSortTest { public static void main(String[] args) { // create object to perform bubble sort BubbleSort sortArray = new BubbleSort(10);
System.out.println("Before:"); // print unsorted array using implicit call to toString method System.out.println(sortArray);
sortArray.sort(); // sort array
System.out.println("After:"); // print sorted array using implicit call to toString method System.out.println(sortArray); } } // end class BubbleSortTest
Exercises 19.5 Bubble Sort) Implement bubble sort -another simple yet inefficient sorting technique.It's called bubble sort or sinking sort because smaller values gradually "bubble" their way to the top of the array (i.e., toward the first element) like air bubbles rising in water, while the larger values sink to the bottom (end) of the array. The technique uses nested loops to make several passes through the array. Each pass compares successive pairs of elements. If a pair is in increasing order (or the values are equal), the bubble sort leaves the values as they are. If a pair is in decreasing order, the bubble sort swaps their values in the array. The first pass compares the first two elements of the array and swaps their values if necessary. It then compares the second and third elements in the array. The end of this pass compares the last two elements in the array and swaps them if necessary. After one pass, the largest element will be in the last index. After two passes, the largest two elements will be in the last two indices. Explain why bubble sort is an O(2) algorithm 19.6 mance of the bubble sort you developed in Exercise 19.5 (Enhanced Bubble Sort) Make the following simple modifications to improve the perfor- a) After the first pass, the largest number is guaranteed to be in the highest-numbered array element; after the second pass, the two highest numbers are "in place"; and so on. In- stead of making nine comparisons on every pass for a ten-element array, modify the bubble sort to make eight comparisons on the second pass, seven on the third pass, an so on The data in the array may already be in proper or near-proper order, so why make nine passes if fewer will suffice? Modify the sort to check at the end of each pass whether any swaps have been made. If there were none, the data must already be sorted, so the pro- gram should terminate. If swaps have been made, at least one more pass is needed. b)Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started