27.3k views
0 votes
//Add you starting comment block public class BubbleBubbleStarter //Replace the word Starter with your initials { public static void main (String[] args) { //Task 1: create an input double array list named mylist with some values pSystem.out.println("My list before sorting is: "); //Task 2: print the original list //Use println() to start and then replace with your printList() method after Task 4a is completed. p//Task 3: call the bubblesort method for mylist p//Task 4b: print the sorted list p} //Task 4a: create a method header named printlist to accept a formal parameter of a double array //create a method body to step through each array element println each element p//printList method header p//for loop p//println statement static void bubbleSort(double[] list) { boolean changed = true; do { changed = false; for (int j = 0; j < list.length - 1; j++) if (list[j] > list[j+1]) { //swap list[j] with list[j+1] double temp = list[j]; list[j] = list[j + 1]; list[j + 1] = temp; changed = true; } } while (changed); } }

User Sanzy
by
4.8k points

1 Answer

6 votes

Answer:

See explaination

Step-by-step explanation:

import java.util.Scanner;

public class BubbleBubbleStarter {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

double arr[] = new double[10];

System.out.println("Enter 10 GPA values: ");

for (int i = 0; i < 10; i++)

arr[i] = sc.nextDouble();

sc.close();

System.out.println("My list before sorting is: ");

printlist(arr);

bubbleSort(arr);

System.out.println("My list after sorting is: ");

printlist(arr);

}

static void bubbleSort(double[] list) {

boolean changed = true;

do {

changed = false;

for (int j = 0; j < list.length - 1; j++) {

if (list[j] > list[j + 1]) {

double temp = list[j];

list[j] = list[j + 1];

list[j + 1] = temp;

changed = true;

}

}

} while (changed);

}

static void printlist(double list[]) {

for (int j = 0; j < list.length; j++) {

System.out.println(list[j]);

}

}

}

User Amin Alaee
by
6.6k points