Question

Create a program called GeneralizedBubbleSort that will make use of the Comparable Interface in the same...

Create a program called GeneralizedBubbleSort that will make use of the Comparable Interface in the same way it is used in the GeneralizedSelectionSort. You should use the attached ComparableDemo to test your program.

public class ComparableDemo

{

public static void main(String[] args)

{

Double[] d = new Double[10];

for (int i = 0; i < d.length; i++)

d[i] = new Double(d.length - i);

System.out.println("Before sorting:");

int i;

for (i = 0; i < d.length; i++)

System.out.print(d[i].doubleValue( ) + ", ");

System.out.println( );

GeneralizedSelectionSort.sort(d, d.length);

System.out.println("After sorting:");

for (i = 0; i < d.length; i++)

System.out.print(d[i].doubleValue( ) + ", ");

System.out.println( );

String[] a = new String[10];

a[0] = "dog";

a[1] = "cat";

a[2] = "cornish game hen";

int numberUsed = 3;

System.out.println("Before sorting:");

for (i = 0; i < numberUsed; i++)

System.out.print(a[i] + ", ");

System.out.println( );

GeneralizedSelectionSort.sort(a, numberUsed);

System.out.println("After sorting:");

for (i = 0; i < numberUsed; i++)

System.out.print(a[i] + ", ");

System.out.println( );

}

}

public class GeneralizedSelectionSort

{

/**

   Precondition: numberUsed <= a.length;

The first numberUsed indexed variables have values.

   Action: Sorts a so that a[0, a[1], ... , a[numberUsed - 1] are in

   increasing order by the compareTo method.

*/

public static void sort(Comparable[] a, int numberUsed)

{

int index, indexOfNextSmallest;

for (index = 0; index < numberUsed - 1; index++)

{//Place the correct value in a[index]:

indexOfNextSmallest = indexOfSmallest(index, a, numberUsed);

interchange(index,indexOfNextSmallest, a);

   //a[0], a[1],..., a[index] are correctly ordered and these are

   //the smallest of the original array elements. The remaining

   //positions contain the rest of the original array elements.

}

}

/**

   Returns the index of the smallest value among

   a[startIndex], a[startIndex+1], ... a[numberUsed - 1]

*/

private static int indexOfSmallest(int startIndex,

Comparable[] a, int numberUsed)

{

Comparable min = a[startIndex];

int indexOfMin = startIndex;

int index;

for (index = startIndex + 1; index < numberUsed; index++)

if (a[index].compareTo(min) < 0)//if a[index] is less than min

{

min = a[index];

indexOfMin = index;

//min is smallest of a[startIndex] through a[index]

}

return indexOfMin;

}   

/**

   Precondition: i and j are legal indices for the array a.

   Postcondition: Values of a[i] and a[j] have been interchanged.

*/

private static void interchange(int i, int j, Comparable[] a)

{

Comparable temp;

temp = a[i];

a[i] = a[j];

a[j] = temp; //original value of a[i]

}   

}

just need some change to the GeneralizedSelectionSort

0 0
Add a comment Improve this question Transcribed image text
Request Professional Answer

Request Answer!

We need at least 10 more requests to produce the answer.

0 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the answer will be notified once they are available.
Know the answer?
Add Answer to:
Create a program called GeneralizedBubbleSort that will make use of the Comparable Interface in the same...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
  • Why are obj1 and obj2 printing the same areas? I'm trying to learn about Comparable interface....

    Why are obj1 and obj2 printing the same areas? I'm trying to learn about Comparable interface. Couldn't figure it out. the compare to method should print 0, 1, or -1 import java.util.*; public class RectangleMain {    public static void main(String [] args) throws CloneNotSupportedException    {        /*ComparableRectangleAlsoCloneable obj1 = new ComparableRectangleAlsoCloneable(4, 5);        ComparableRectangleAlsoCloneable obj2 = (ComparableRectangleAlsoCloneable)obj1.clone();               System.out.println(obj1.toString());        System.out.println(obj1 == obj2); //false        System.out.println(obj2.toString());*/               Scanner...

  • Draw a flowchart for this program public class InsertionSort {     public static void main(String a[]) {...

    Draw a flowchart for this program public class InsertionSort {     public static void main(String a[]) {         int[] array = {7, 1, 3, 2, 42, 76, 9};         insertionSort(array);     }     public static int[] insertionSort(int[] input) {         int temp;         for (int i = 1; i < input.length; i++) {             for (int j = i; j > 0; j--) {                 if (input[j] < input[j - 1]) {                     temp = input[j];                     input[j] = input[j - 1];                     input[j - 1] = temp;                 }             }             display(input, i);...

  • A test harness program for testing sorting methods is provided with the rest of the textbook...

    A test harness program for testing sorting methods is provided with the rest of the textbook program files. It is the file Sorts.java in the ch11 package. The program includes a swap method that is used by all the sorting methods to swap array elements. Describe an approach to modifying the program so that after calling a sorting method the program prints out the number of swaps needed by the sorting method. Implement your approach. Test your new program by...

  • Modify the sorts (selection sort, insertion sort, bubble sort, quick sort, and merge sort) by adding code to each to tally the total number of comparisons and total execution time of each algorithm. E...

    Modify the sorts (selection sort, insertion sort, bubble sort, quick sort, and merge sort) by adding code to each to tally the total number of comparisons and total execution time of each algorithm. Execute the sort algorithms against the same list, recording information for the total number of comparisons and total execution time for each algorithm. Try several different lists, including at least one that is already in sorted order. ---------------------------------------------------------------------------------------------------------------- /** * Sorting demonstrates sorting and searching on an...

  • The file Sorting.java contains the Sorting class from Listing 9.9 in the text. This class implements...

    The file Sorting.java contains the Sorting class from Listing 9.9 in the text. This class implements both the selection sort and the insertion sort algorithms for sorting any array of Comparable objects in ascending order. In this exercise, you will use the Sorting class to sort several different types of objects. 1. The file Numbers.java reads in an array of integers, invokes the selection sort algorithm to sort them, and then prints the sorted array. Save Sorting.java and Numbers.java to...

  • // CMPS 390 // MinHeap.java // Complete 4 methods: getMin, add, removeMin, reheap public class MinHeap...

    // CMPS 390 // MinHeap.java // Complete 4 methods: getMin, add, removeMin, reheap public class MinHeap { private Comparable a heap; // array of heap entries private static final int DEFAULT MAX SIZE = 100; private int lastIndex; // index of last entry public MinHeap() { heap = new Comparable (DEFAULT_MAX_SIZE]; lastIndex = 0; } // end default constructor public MinHeap (int maxSize) { heap = new Comparable (maxSize); lastIndex = 0; } // end constructor public MinHeap (Comparable[] entries)...

  • the code needs to be modifed. require a output for the code Java Program to Implement...

    the code needs to be modifed. require a output for the code Java Program to Implement Insertion Sort import java.util.Scanner; /Class InsertionSort * public class Insertion Sort { /Insertion Sort function */ public static void sort( int arr) int N- arr.length; int i, j, temp; for (i-1; i< N; i++) j-i temp arrli; while (j> 0 && temp < arrli-1) arrli]-arrli-1]; j-j-1; } arrlj] temp; /Main method * public static void main(String [] args) { Scanner scan new Scanner( System.in...

  • Directions: Follow the instructions below to explore sorting objects using boxes below. You will sort the...

    Directions: Follow the instructions below to explore sorting objects using boxes below. You will sort the boxes by the largest volume. Copy and Paste the Box method below into BlueJ public class Box { private double length, height, depth;    public Box( double length, double height, double depth ) { this.length = length; this.height = height; this.depth = depth; }    public double volume() { return length*height*depth; } // compare this Box to another Box int compareTo( Box other )...

  • Hello this is my java sorting algorithm program i need all of my errors corrected so...

    Hello this is my java sorting algorithm program i need all of my errors corrected so I can run it. thank you!! import java.util.Scanner;    public class SortingAlogs { public static void main(String[]args){ int array [] = {9,11,15,34,1}; Scanner KB = new Scanner(System.in); int ch; while (true) { System.out.println("1 Bubble sort\n2 Insertion sort\n3 Selection sort\n"); ch = KB.nextInt(); if (ch==1)    bubbleSort(array); if (ch==2)    insertion(array); if (ch==3)    Selection(array); if (ch==4) break;    print(array);    System.out.println(); }    }...

  • need help editing or rewriting java code, I have this program running that creates random numbers...

    need help editing or rewriting java code, I have this program running that creates random numbers and finds min, max, median ect. from a group of numbers,array. I need to use a data class and a constructor to run the code instead of how I have it written right now. this is an example of what i'm being asked for. This is my code: import java.util.Random; import java.util.Scanner; public class RandomArray { // method to find the minimum number in...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT