Write a Java Program to input 10 book names and their corresponding price in two
arrays and arrange the books in ascending order along with the price. Display the sorted
array along with the price using Bubble Sort Technique.
I will mark the answer as brainliest!!
Answers
You can print the sorted array on your own....I was not having space to do it.....I hope it helps
A Java Program to input 10 book names and their corresponding price in two arrays and arrange the books in ascending order along with the price and display the sorted array along with the price using Bubble Sort Technique as follows:
import java.util.*;
class Arrays
{
public static void main( String[] args )
{
Scanner scan = new Scanner(System.in);
String Book[]= new String[10];
String t = " ";
int i = 0, j = 0, k = 0;
int price = new int[10];
for(i = 0; i<10; i++)
{
System.out.println ("Enter the book: ");
Book[i] = scan.nextline();
System.out.println ("Enter the price: ");
price[i] = scan.int();
}
for(i = 0; i < Book.length - 1; i++)
{
for(j = 0; j < Book.length - 1 - i; j++)
{
if( Book[j] compareTo Book[j+1] > 0)
{
t = n[j];
n[j] = n[j+1];
n[j + 1] = t;
k = price[j];
price[j] = price[j+1];
price[j+1] = k;
}
}
}
}
}
- Here, we have a program where we take user inputs and arrange the books in an ascending order along with their respective prices. It then displays the whole array using the println() function.
- We also compare using bubble sort and sort the arrays into ascending order and use a temporary variable t to sort the array.
#SPJ2