I need help understanding why my mergeSort() method does not return a sorted array when it returns to the caller. I've compared this to similar source code online and I can't tell what I'm doing wrong. I had to make a Comparable [] array to receive the sorted array from the method but that shouldn't be necessary, right?
public class Merging {
/**
* Entry point for sample output.
*
* @param args the command line arguments
*/
public static void main(String[] args) {
Queue q1 = new ListQueue(); q1.enqueue("T"); q1.enqueue("R"); q1.enqueue("O"); q1.enqueue("L"); q1.enqueue("E");
Queue q2 = new ListQueue(); q2.enqueue("X"); q2.enqueue("S"); q2.enqueue("P"); q2.enqueue("M"); q2.enqueue("E"); q2.enqueue("A");
System.out.println(q1.toString());
System.out.println(q2.toString());
//Q1
Queue merged = mergeQueues(q1, q2);
System.out.println(merged.toString());
//Q2
String[] a = {"S", "O", "R", "T", "E", "X", "A", "M", "P", "L", "E"};
mergeSort(a);
show(a);
Comparable[] a1 = mergeSort(a);//HOW TO AVOID THIS???
System.out.println(isSorted(a1));
System.out.println(isSorted(a));
show(a1);
//Unsorted array
Integer[] numArr = {2, 6, 3, 5, 1};
//Call merge sort
mergeSort(numArr);
show(numArr);
//Q3
String[] b = {"S", "O", "R", "T", "E", "X", "A", "M", "P", "L", "E"};
shuffle(b);
show(b);
shuffle(b);
show(b);
}
public static Queue mergeQueues(Queue q1, Queue q2) {
Queue aux = new ListQueue();
while((!q2.isEmpty()) || (!q1.isEmpty())) {
if (q1.isEmpty())
while(!q2.isEmpty())
aux.enqueue(q2.dequeue());
else if(q2.isEmpty())
while(!q1.isEmpty())
aux.enqueue(q1.dequeue());
Get Free Quote!
255 Experts Online