Write a program that implements a method that receives an array parameter and so
ID: 3647012 • Letter: W
Question
Write a program that implements a method that receives an array parameter and sorts that array using the bubble-sort algorithm. The bubble-sort algorithm makes several passes through the array. On each pass, successive neighboring pairs are compared. If a pair is in decreasing order, its values are swapped: otherwise, the values remain unchanged. The technique is called a bubble sort because the smaller values gradually "bubble" their way to the top.The algorithm may be described as follows:
boolean changed;
do{
changed = false;
for(int i = 0; i < list.length - 1; i++){
if(list[i] > list[i + 1]){
swap list[i] with list[i + 1];
changed = true;
}
}
}while(changed);
Explanation / Answer
import java.util.Random; public class ArrayMethodAlgorithm { public static void main(String[] args) { int x = 1; // creates an int value int[] y = new int[10]; // y represents an array of int values Random rndNumGen = new Random(10); // generates a random number m(x, y); // Invoke m with arguments x and y } public static void m(int number, int[] numbers) { number = 1337; // Assign a new value to number numbers[0] = 5432; // Assign a new value to numbers[0] boolean changed; do{ changed = false; for(int i = 0; i list[i + 1]){ swap list[i] with list[i + 1]; changed = true; } } }while(changed); } }Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.