Answered step by step
Verified Expert Solution
Question
1 Approved Answer
import java.util.Scanner; public class QuickSortBook { private B []a; int n; char answer; public static void main(String[] args) { System.out.println(Do you want to add books
import java.util.Scanner;
public class QuickSortBook> {
private B []a;
int n;
char answer;
public static void main(String[] args) {
System.out.println("Do you want to add books into the library? Yes or No:");
Scanner yn = new Scanner(System.in);
char answer = yn.next().charAt(0); // Read user input
if(answer=='y' || answer=='Y') {
System.out.println("How many books do you want to add?");
Scanner num = new Scanner(System.in);
int n = num.nextInt();
Book []books = new Book[5+n];
Book book = new Book("John","Carter", "Mathematics", 115);
books[0] = book;
book = new Book("Mary","Carter", "Biology", 117);
books[1] = book;
book = new Book("Alex","Lumb", "Physics", 111);
books[2] = book;
book = new Book("David","Johnson","Chemistry",112);
books[3] = book;
book = new Book("Shaun","Smith","Computers",114);
books[4] = book;
//insert book into array
for(int i = 5; i <5+n; i++) //current index
{
System.out.println("what is the author's name, book title and book code ?");
Scanner FName = new Scanner(System.in);
String authorFName = FName.nextLine();
Scanner LName = new Scanner(System.in);
String authorLName = LName.nextLine();
Scanner Title = new Scanner(System.in);
String bookTitle = Title.nextLine();
Scanner code = new Scanner(System.in);
int bookCode = code.nextInt();
book = new Book(authorFName,authorLName,bookTitle, bookCode);
books[i]= book;
}
QuickSortBookex = new QuickSortBook<>();
// Assigned array
ex.a = books;
System.out.println(" List of books: ");
// prints the given array
ex.printArray();
// sort the array
ex.sort();
System.out.println(" List of books according to the book code: ");
//prints the sorted array
ex.printArray();
}
else if(answer=='n' || answer=='N') {
Book []books = new Book[5];
Book book = new Book("John","Carter", "Intro to Maths", 115);
books[0] = book;
book = new Book("Mary","Carter", "Biology", 117);
books[1] = book;
book = new Book("Alex","Lumb", "Physics", 111);
books[2] = book;
book = new Book("David","Johnson","Chemistry",112);
books[3] = book;
book = new Book("Shaun","Smith","Computers",114);
books[4] = book;
QuickSortBookex = new QuickSortBook<>();
// Assigned array
ex.a = books;
System.out.println(" List of books: ");
// prints the given array
ex.printArray();
// sort the array
ex.sort();
System.out.println(" List of books according to the book code: ");
//prints the sorted array
ex.printArray();
}
}
// This method sort an array and internally calls quickSort
public void sort(){
int left = 0;
int right = a.length-1;
quickSort(left, right);
}
// This method is used to sort the array using quicksort algorithm.
// It takes left and the right end of the array as two cursors
private void quickSort(int left,int right){
// If both cursor scanned the complete array quicksort exits
if(left >= right)
return;
// Pivot using median of 3 approach
B pivot = getMedian(left, right);
int partition = partition(left, right, pivot);
// Recursively, calls the quicksort with the different left and right parameters of the sub-array
quickSort(left, partition-1);//partition -1 refers to the index b4 partition (to sorted sub-array)
quickSort(partition+1, right);//partition +1 refers to the index after partition (to sorted sub-array)
}
// This method is used to partition the given array and returns the integer which points to the sorted pivot index
private int partition(int left,int right,B pivot){
int leftCursor = left-1;
int rightCursor = right;
while(leftCursor < rightCursor){
while(leftCursor )a[++leftCursor]).compareTo(pivot) < 0);
while(rightCursor > 0 && ((Comparable)a[--rightCursor]).compareTo(pivot) > 0);
if(leftCursor >= rightCursor){
break;
}else{
swap(leftCursor, rightCursor);
}
}
swap(leftCursor, right);
return leftCursor;
}
public B getMedian(int left,int right){
int center = (left+right)/2;
if(((Comparable)a[left]).compareTo(a[center]) > 0)
swap(left,center);
if(((Comparable)a[left]).compareTo(a[right]) > 0)
swap(left, right);
if(((Comparable)a[center]).compareTo(a[right]) > 0)
swap(center, right);
return a[right];
}
// This method is used to swap the values between the two given index
public void swap(int left,int right){
B temp = a[left];
a[left] = a[right];
a[right] = temp;
}
public void printArray(){
for(B i : a){
System.out.println(i+" ");
}
}
}
public void setauthorFName(String authorFName) {
this.authorFName = authorFName;
}
public String getauthorLName() {
return authorLName;
}
public void setauthorLName(String authorLName) {
this.authorLName = authorLName;
}
public String getbookTitle() {
return bookTitle;
}
public void setbookTitle(String bookTitle) {
this.bookTitle = bookTitle;
}
public int getbookCode() {
return bookCode;
}
public void setbookCode(int bookCode) {
this.bookCode = bookCode;
}
public String toString() {
return "Book Code: "+getbookCode()+" Book Title:"+getbookTitle() + " Author:"+getauthorFName()+" "+getauthorLName();
}
public int compareTo(Book o) {
Book e = (Book)o;
if(this.bookCode > e.getbookCode())
return 1;
if(this.bookCode < e.getbookCode())
return -1;
if(this.bookCode == e.getbookCode())
return 0;
return 0;
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started