I have a program that sorts an array of Comparable[] objects however in my main
ID: 3863228 • Letter: I
Question
I have a program that sorts an array of Comparable[] objects however in my main method the String[] that I'm passing to the method does not return the sorted version of the Comparable[].
What am I doing wrong?
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"};
// MUST RETURN SORTED ARRAY sort(a);
// I did this just now
Comparable [] a1 = sort(a);
// Is this considered "clean code"?
System.out.println(isSorted(a));
show(a);
//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());
else if(((Comparable)q1.front()).compareTo(q2.front()) < 0){
aux.enqueue(q2.front());
q2.dequeue();
}
else if(((Comparable)q1.front()).compareTo(q2.front()) > 0){
aux.enqueue(q1.front());
q1.dequeue();
}
else
{
aux.enqueue(q1.front());
aux.enqueue(q2.front());
q1.dequeue();
q2.dequeue();
}
}
//TODO: implement this.
return aux;
}
public static void sort(Comparable[] a) {
a = mergeSort(a);
}
//helper
public static Comparable[] mergeSort(Comparable[] a) {
//base case
if(a.length <= 1)
return a;
int mid = a.length/2;
Comparable[] left = new Comparable[mid];
Comparable[] right = new Comparable[a.length - mid];
Comparable[] aux = new Comparable[a.length];
for(int i = 0; i < mid; i++)
left[i] = a[i];
int x = 0;
for(int i = mid; i < a.length; i++){
if(x < right.length){
right[x] = a[i];
x++;
}
}
left = mergeSort(left);
right = mergeSort(right);
aux = merge(left, right);
return aux;
}
//merge method
public static Comparable[] merge(Comparable[] a, Comparable[] b){
Comparable[] aux = new Comparable[a.length + b.length];
int indexL = 0;
int indexR = 0;
int indexAux = 0;
while(indexL < a.length || indexR < b.length){
if(indexL < a.length && indexR < b.length){
if(((Comparable)a[indexL]).compareTo(b[indexR]) < 0){
aux[indexAux] = a[indexL];
indexL++;
indexAux++;
}
else if(((Comparable)a[indexL]).compareTo(b[indexR]) > 0){
aux[indexAux] = b[indexR];
indexR++;
indexAux++;
}
//The two values in the array are equal
else{
aux[indexAux] = a[indexL];
indexAux++;
aux[indexAux] = b[indexR];
indexAux++;
indexL++;
indexR++;
}
}
else if(indexL < a.length && indexR >= b.length){
aux[indexAux] = a[indexL];
indexAux++;
indexL++;
}
else if(indexR < b.length && indexL >= a.length){
aux[indexAux] = b[indexR];
indexAux++;
indexR++;
}
}
return aux;
}
public static void shuffle(Object[] a) {
//TODO: implement this.
}
//sorting helper from text
private static boolean less(Comparable v, Comparable w) {
return v.compareTo(w) < 0;
}
//sorting helper from text
private static void show(Comparable[] a) {
for (Comparable a1 : a)
System.out.print(a1 + " ");
System.out.println();
}
//sorting helper from text
public static boolean isSorted(Comparable[] a) {
for (int i = 1; i < a.length; i++)
if (less(a[i], a[i-1]))
return false;
return true;
}
Explanation / Answer
Hello, this program has some issue in the sorting code as bellow:-
while(indexL < a.length || indexR < b.length){
if(indexL < a.length && indexR < b.length){
if(((Comparable)a[indexL]).compareTo(b[indexR]) < 0){
aux[indexAux] = a[indexL];
indexL++;
indexAux++;
}
else if(((Comparable)a[indexL]).compareTo(b[indexR]) > 0){
aux[indexAux] = b[indexR];
indexR++;
indexAux++;
}
//The two values in the array are equal
else{
aux[indexAux] = a[indexL];
indexAux++;
aux[indexAux] = b[indexR];
indexAux++;
indexL++;
indexR++;
}
}
Correct code:-
while(indexL > a.length || indexR > b.length){
if(indexL > a.length && indexR > b.length){
if(((Comparable)a[indexL]).compareTo(b[indexR]) > 0){
aux[indexAux] = a[indexL];
indexL++;
indexAux++;
}
else if(((Comparable)a[indexL]).compareTo(b[indexR]) < 0){
aux[indexAux] = b[indexR];
indexR++;
indexAux++;
}
//The two values in the array are equal
else{
aux[indexAux] = a[indexL];
indexAux++;
aux[indexAux] = b[indexR];
indexAux++;
indexL++;
indexR++;
}
}
I hope this works for you thanks
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.