JAVA. Build a program that will search and sort numbers using bubble sort. The p
ID: 3742431 • Letter: J
Question
JAVA. Build a program that will search and sort numbers using bubble sort. The program will have a Database class and a Main class.
The user will input five random numbers in the main class to be stored in an array. Include an instance of the data object within the main class to store the numbers array in a field of the database using its constructor.
Data Class guidelines :
Use a bubble sort method that returns the data in ascending and descending. Call the method from the main and print the result.
Use methods that will return min, max, and average. Call them from the main class and print.
Use a method that will search for a value via binary search and returns the index of the value. Call the method from the main class and print.
Explanation / Answer
Answer:
public class Order {
public static void main(String[] args)
{
int arr[] ={860,8,200,9};
System.out.println("---input BEFORE Bubble Sort---");
displayfunction(arr);
Order(arr);
System.out.println("---input AFTER Bubble Sort---");
displayfunction(arr);
}
static void Order(int[] input)
{
int n = input.length;
int value= 0;
for(int i=0; i < n; i++)
{ System.out.println("Sort Pass Number "+(i+1));
for(int j=1; j < (n-i); j++)
{
System.out.println("Comparing "+ input[j-1]+ " and " + input[j]);
if(input[j-1] > input[j])
{
value= input[j-1];
input[j-1] = input[j];
input[j] = temp;
System.out.println(input[j] + " is greater than " + input[j-1]);
System.out.println("Swapping Elements: New input After Swap");
displayfunction(input);
}
}
}
}
static void displayfunction(int[] input){
for(int i=0; i < input.length; i++)
{
System.out.print(input[i] + " ");
}
System.out.println();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.