int compares (Card cl, Card c2) This method takes two card objects and returns -1, 0, or 1 if c1 is smaller, equal to, or greater than c2, respectively. You need to compare their suits first and if they have the same suit, then compare their ranks. void quickSort (Card [] cardArray, int first, int last) This method takes a card array as well as two indices. It sorts the cards in the range specified by first and last with quick sort. It first calls the split method to partition the elements in the range into two subarrays, and sorts both subarrays recursively. It repeats this process until the entire array is sorted. int split (Card [] cardArray, int first, int last) This is a helper method for quick sort which takes a card array and two indices. It begins by selecting the first element in the card array as the pivot, and then moves elements so that everything in the left subarray is smaller than the pivot and everything on the right is greater than the pivot. The method returns the final index of the pivot. You will need to call the compares method to make comparisons between cards.
I need help implementing those functions.
public class DeckCards
{
// card comparison
public int compares(Card c1, Card c2)
{
return - 1; // replace this statement with your own return
}
// This is the wrapper method for quick sort
// Do not make any changes to this method!
public void quickSort(Card[] cardArray)
{
quickSortRec(cardArray, 0, cardArray.length - 1);
}
// This is the recursive helper method for quickSort
public void quickSortRec(Card[] cardArray, int first, int last)
{
// TODO: implement this method
}
// This method splits a Card array into two sections, with all the elements less
// than the pivot in the left section and all the elements greater than the pivot
// in the right section
public int split(Card[] cardArray, int first, int last)
{
// TODO: implement this method
return -1; // replace this statement with your own return
}
// This method takes an int array which represents a complete binary tree as
// the parameter. It checks if the tree stored in the array is a heap.
public boolean isHeap(int[] A)
{
// TODO: implement this method
return false; // replace this statement with your own return
}
}
Trending now
This is a popular solution!
Step by step
Solved in 3 steps
Unfortunately, the code itself executes but recursively in infinite loop. Is there a reason why it is doing that?