Write a program that implements a method that receives an array parameter and so
ID: 3623440 • 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);
Continue to properly document your source code. Write this program as if you were explaining it to someone new to arrays. Fully document your code in such a way newcomers to Java will understand and be able to implement a Java array. Your grade on this assignment will be based on your thoroughness of documentation as well as you correctness of code.
Explanation / Answer
please rate - thanks
import java.util.*;
public class sort
{public static void main(String[] args)
{int choice,n,i,j,rows,more;
Scanner input = new Scanner(System.in);
Random gen = new Random();
System.out.println("How many numbers? ");
n=input.nextInt();
int num[]=new int[n];
for(i=0;i<n;i++)
num[i]=gen.nextInt(1000);
print(num,n,"numbers before sorted");
sort(num);
print(num,n,"numbers after sorted");
}
public static void print(int num[],int n,String mess)
{int j;
System.out.println(mess);
for(j=1;j<=n;j++)
{ System.out.print(num[j-1]+" ");
if(j%20==0)
System.out.println(" ");
}
System.out.println(" ");
}
public static void sort(int list[])
{int t;
boolean changed;
do{
changed = false;
for(int i = 0; i < list.length - 1; i++){
if(list[i] > list[i + 1]){
t=list[i];
list[i]=list[i+1];
list[i+1]=t;
changed = true;
}
}
}while(changed);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.