TheDeadline
TheDeadline

Reputation: 85

Quicksort an array of objects

EDIT: I called my sorting method "quicksort" before but now thanks to you guys, I realize that was completely wrong. This is likely some type of selection sorting.

Here in this constructor, I have an array of cards (the object array) and an int array that should correspond to the object array:

public Deck ()
{
    deck = new Card [52];
    deckNum = new int [52];

    for (int x = 0 ; x < 52 ; x++)  // for each card in standard deck
    {
        deck [x] = new Card (x); // create card
        deckNum [x] = x; // card number
    }

}

After that, I have a shuffle method which shuffles correctly so I also made it shuffle the corresponding int array:

 public void shuffle ()
{
    int value;
    int tempNum;
    Card temp;
    Random r = new Random();
    for (int x = 0; x < deck.length; x++) // goes through all cards
    {
      value = r.nextInt(x + 1); // random value

      //Shuffle Card array
      temp = deck[value]; // new array
      deck[value] = deck[x];
      deck[x] = temp;

   // Shuffle int array (not displayed)
      tempNum = deckNum[value];
      deckNum[value] = deckNum[x];
      deckNum[x] = tempNum;


    }
}

Now for the sorting method, which when I try to use, ends up shuffling the cards again.

  public void quickSort ()
{
    Card temp1;

    for (int x = 0 ; x < deck.length - 1 ; x++) //sort first length-1 values
    {
       int lowPos = x; // assume first value is lowest

  for (int y = x + 1 ; y < deck.length ; y++) {// check rest of list
    if (deckNum [y] < deckNum [lowPos]) // uses the int array to find a lower value
    {
    lowPos = y;// make it the lowest
    }
 }


      // swap low value with value in its proper position
      //uses card array again
      temp1 = deck [x]; 
      deck [x] = deck [lowPos];
      deck [lowPos] = temp1;
    }
}

You may only look through the quicksort method, but just in case, deckNum[] is the array that holds the integer values of the cards while the deck[] array holds the card objects which I am trying to manipulate.

The sorting method works when I use just an int array.

I may be completely wrong in using an int array but I honestly don't know any other way. A little help would be great!

Upvotes: 4

Views: 1969

Answers (1)

jrook
jrook

Reputation: 3519

The reason the array is not sorted is that the deckNum array is not sorted as the loop goes forward.

Try this:

public void quickSort () {
    Card temp1;
    int temp2;

    for (int x = 0 ; x < deck.length - 1 ; x++) //sort first length-1 values
    {
        int lowPos = x; // assume first value is lowest
        for (int y = x + 1 ; y < deck.length ; y++) // check rest of list
            if (deckNum [y] < deckNum [lowPos])
                lowPos = y; // make it the lowest

        temp1 = deck [x]; 
        deck [x] = deck [lowPos];
        deck [lowPos] = temp1;

        //Add the following to your code
        temp2 = deckNum [x]; 
        deckNum [x] = deckNum [lowPos];
        deckNum [lowPos] = temp2;
    }
}

As a side note, what you are implementing is NOT the quick sort algorithm. Take a look at the Wikipedia article referred to in the comments to learn how quick sort actually works.

Upvotes: 1

Related Questions