Question
Help me with my error for Java. I am supposed to: Implement the recursive algorithm for merge sort. Complete the implementation of quick sort by
Help me with my error for Java. I am supposed to: Implement the recursive algorithm for merge sort. Complete the implementation of quick sort by implementing the method partition.
This is the textbook answer.
https://www.chegg.com/homework-help/Data-Structures-and-Abstractions-with-Java-4th-edition-chapter-9-problem-1P-solution-9780133744057
I have copied it exactly into an IDE, but yet there is an error. I have bolded where the error is.
The error is in the second else if statement. else if (x[q]) load [r] = x[q++];
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
import java.io.*; import java.util.Scanner;
public class RecursiveMergesort { public static void merge(int[] x, int front, int rear) { int length = rear - front; if (length <= 1) return; int centroid = front + length/2; merge(x, front, centroid); merge(x, centroid, rear); int[] load = new int[length]; int p = front, q = centroid; for (int r=0; r < length; r++) { if (p==centroid) load[r] = x[q++]; else if (q==rear) load[r] = x[p++]; else if (x[q]) load[r] = x[q++]; else load[r] = x[p++]; } // End for loop for (int r = 0; r < length; r++) x[front + r] = load[r]; } // End merge
public static void main(String[] args) { Scanner search = new Scanner(System.in); System.out.println("Implementation of Merge Procedure "); System.out.println("Recursive Merge Call "); int z, p; System.out.println("Total number of inputs in list "); z = search.nextInt(); int value[] = new int[z]; System.out.println(" Input " + z + " positive integer values "); for (p=0; p value[p] = search.nextInt(); merge(value, 0, z); System.out.println(" The sorted list after processing the algorithm "); for (p=0; p< z; p++) System.out.println(value[p] + ""); } // End main } // End RecursiveMergesort class
----------------NEW CLASS------------------ import java.io.*; import java.util.Scanner;
public class quicksort { public static void value(int[] list) { partition(list, 0, list.length -1); } // end value
public static void partition(int list[], int front, int rear) { int x = front, y = rear; int load; int middle = list[(front+rear)/2]; while (x <= y) { while (list[x] < middle) x++; while (list[y] > middle) y--; if (x <=y) { load = list[x]; list[x] = list[y]; list[y] = load; x++; y--; } } // End while loop if (front < y) partition(list, front, y); if (x < rear) partition(list, x, rear); } // end partition
public static void main(String[] args) { Scanner search = new Scanner(System.in); System.out.println("Implementation of Quick Sort Procedure "); System.out.println("Recursive Quick Sort Call "); int z, x; System.out.println("Total number of inputs in list "); z = search.nextInt(); int list[] = new int[z]; System.out.println(" Input " + z + " positive integer values."); for (x = 0; x < z; x++) list[x] = search.nextInt(); value(list); System.out.println(" The sorted list after processing the algorithm "); for (x=0; x System.out.println(list[x] + " "); } // End main } // End quicksort class
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