Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Introduction to Java Programming. Write your own source code with plenty of comm

ID: 3790734 • Letter: I

Question

Introduction to Java Programming. Write your own source code with plenty of comments.

(Bubble sort) Write a sort method that uses the bubble-sort algorithm. The bubblesort algorithm makes several passes through the array. On each pass, successive neighboring pairs are compared. If a pair is not in order, its values are swapped; otherwise, the values remain unchanged. The technique is called a bubble sort or sinking sort because the smaller values gradually “bubble” their way to the top and the larger values “sink” to the bottom. Write a test program that reads in ten double numbers, invokes the method, and displays the sorted numbers.

Console terminated> ProgrammingEx7-18 va Al cation] F Enter ten numbers 0 35 4 70 1 2 10 90 The array after sort is 1.0 0.0 0.0 2.0 3.0 4.0 5.0 7.0 10.0 90.0

Explanation / Answer

public class Bubble {

public static void main(String[] args) {

double[] numbers = new double[100];

for (int i = 0; i < numbers.length ; i++) {
numbers[i] = Math.random() * 200;
}
bubbleSort(numbers);
printArray(numbers, 10);
}

public static void printArray(double[] array, int numberPerLine) {

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

System.out.printf("%4.0f ", array[i]);
if ((i + 1) % numberPerLine == 0) System.out.println("");
}
}

public static void bubbleSort(double[] numbers) {

boolean change;
do {
change = false;
for (int i = 0; i < numbers.length - 1; i++) {

if (numbers[i] > numbers[i + 1]) {
double temp = numbers[i];
numbers[i] = numbers[i + 1];
numbers[i + 1] = temp;
change = true;
}
}

} while (change);

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote