Question
having a little trouble trying to sort ADT objects. I feel like I have 85% of this down but I'm stuck when trying to sort
having a little trouble trying to sort ADT objects. I feel like I have 85% of this down but I'm stuck when trying to sort a ADT Queue by alphabetical order. Here is what I have. What should my driver Code look like and is there anything i need to change?
------------------------------------------------------
package Portfolio;
import java.util.EmptyStackException;
public class Queue { private T[] queue; private int frontIndex; private int backIndex; private static final int DEFAULT_INTIIAL_CAPACITY = 50; public Queue(){ this(DEFAULT_INTIIAL_CAPACITY); } public Queue(int initialCapacity) { @SuppressWarnings("unchecked") T[] tempQueue = (T[]) new Object[initialCapacity + 1]; queue = tempQueue; backIndex = initialCapacity; frontIndex = 0; } public T[] getQueue() { return queue; } Boolean isEmpty() { return frontIndex == ((backIndex +1 ) % queue.length); } void enqueue (T newEntry) { ensureCapacity(); backIndex = (backIndex + 1) % queue.length; queue[backIndex] = newEntry; } private void ensureCapacity() { if (frontIndex == ((backIndex+2) % queue.length)) { T[] oldQueue = queue; int oldSize = oldQueue.length; @SuppressWarnings("unchecked") T[]tempQueue = (T[]) new Object[2 * oldSize]; queue = tempQueue; for (int index = 0; index < oldSize - 1; index++) { queue[index] = oldQueue[frontIndex]; frontIndex = (frontIndex +1) % oldSize; } frontIndex = 0; backIndex = oldSize %2; } } T dequeue() { T front = null; if(!isEmpty()) { front = queue[frontIndex]; queue[frontIndex] = null; frontIndex = (frontIndex + 1) % queue.length; } return front; } T getFront(){ if(isEmpty()) throw new EmptyStackException(); else return queue[frontIndex]; } public T[] toArray() { @SuppressWarnings("unchecked") T[]result = (T[])new Object[DEFAULT_INTIIAL_CAPACITY]; for (int i = 0; i < queue.length;i++) { result[i] = queue[i]; } return result; }
}
-------------------------------------------------------------------------------------
package Portfolio;
public class Person { private String lastName,firstName; private int age; Person(String firstName, String lastName, int age){ this.setFirstName(firstName); this.setLastName(lastName); this.setAge(age); }
public String getLastName() { return lastName; }
public void setLastName(String lastName) { this.lastName = lastName; }
public String getFirstName() { return firstName; }
public void setFirstName(String firstName) { this.firstName = firstName; }
public int getAge() { return age; }
public void setAge(int age) { this.age = age; } public String toString() { return getLastName() + ", " + getFirstName() + " - " + getAge(); }
} ----------------------------------------------------------------
package Portfolio;
public class Sort { private static final int MIN_SIZE = 4;
private static > void sortFirstMiddleLast (T[] a, int first, int mid, int last) { if (a[first].compareTo(a[mid]) > 0) swap ( a, first, mid); if (a [mid].compareTo(a[last]) > 0) { swap (a, mid, last); if( a[first].compareTo(a[mid]) > 0) { swap(a,first,mid); } } } private static void swap(Object[] array, int i, int j) { Object temp = array[i]; array[i] = array[j]; array[j] = temp; } private static > int partition (T[] a, int first, int last) { int mid = (first + last) / 2; sortFirstMiddleLast (a,first,mid,last); swap (a, mid, last - 1); int pivotIndex = last - 1; T pivot = a [pivotIndex]; int indexFromLeft = first + 1; int indexFromRight = last - 2; boolean done = false; while (!done) { while(a[indexFromLeft].compareTo(pivot) < 0) indexFromLeft++; while(a[indexFromRight].compareTo(pivot) > 0) indexFromRight--; if (indexFromLeft < indexFromRight) { swap(a,indexFromLeft,indexFromRight); indexFromLeft++; indexFromRight--; } else done = true; } swap(a,pivotIndex,indexFromLeft); return pivotIndex; } public static > void quickSort(T[] a , int first, int last) { if (last - first + 1 < MIN_SIZE) selectionSort(a); else { int pivotIndex = partition(a, first, last); quickSort(a, first, pivotIndex - 1); quickSort(a,pivotIndex + 1, last); } } public static > void selectionSort(T[] a) { int last = a.length - 1; for (int index = 0; index < last ; index++) { int indexNextSmallest = getIndexOfSmallest(a,index,last); swap(a,index,indexNextSmallest); } } public static > int getIndexOfSmallest(T[] a, int first, int last) { T min = a [first]; int indexOfMin = first; for (int index = first + 1; index <=last ; index++) { if (a[index].compareTo(min) < 0) { min = a[index]; indexOfMin = index; } } return indexOfMin; } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Heres a refined response incorporating the valuable insights from the ratings and addressing potenti...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