Well I finished implementing it with a random pivot and insertion sort and this is what I got.
Code:
void quickSort(struct Card *hand, int start, int end)
{
if(start < end)
{
if((end - start) < MINIMUMNUMBER) // If it is less than 10 cards, do insertion sort.
{
insertionSort(hand, start, end + 1);
}
else
{
int partitionIndex = randomPartition(hand, start, end);
quickSort(hand, start, partitionIndex - 1);
quickSort(hand, partitionIndex + 1, end);
}
}
}
int randomPartition(struct Card *hand, int start, int end)
{
int pivotIndex = start + rand() % (end - start + 1);
const int *pivotPtr = hand[pivotIndex].faceValue;
int partitionIndex, index;
swapCards(&hand[pivotIndex], &hand[end]);
partitionIndex = start;
for(index = start; index < end; index++)
{
const int *temp = hand[index].faceValue;
if((*temp) <= (*pivotPtr))
{
swapCards(&hand[index], &hand[partitionIndex]);
partitionIndex++;
}
}
swapCards(&hand[end], &hand[partitionIndex]);
return partitionIndex;
}
void insertionSort(struct Card *hand, int start, int end)
{
int index1;
for(index1 = start + 1; index1 < end; index1++)
{
struct Card temp = hand[index1];
int index2;
for(index2 = index1 - 1; index2 >= 0 && *(temp.faceValue) < *(hand[index2].faceValue); index2--)
{
hand[index2 + 1] = hand[index2];
hand[index2] = temp;
}
}
}
It works and sorts a deck of cards pretty nicely. Now what I want to do is to make this generic and my swap also generic because as it stands, it can only swap cards
Here is how the swap looks if you want to see
Code:
void swapCards(struct Card *card, struct Card *cardToSwap)
{
/*Create a struct to temporarily hold data*/
struct Card temp;
temp = *card;
*card = *cardToSwap;
*cardToSwap = temp;
}
What would be a seemingly good way to do this in C?