Skip to main content
added homework tag
Link
Jesse C. Slicer
  • 14.2k
  • 1
  • 39
  • 52
Tweeted twitter.com/#!/StackCodeReview/status/205210328370388992
Source Link

Insertion vs Selection

I currently have a selection sorting algorithm, but need some help turning it into a insertion sort, I understand a insertion sort if faster? The hand array is full, and it needs to sort from lowest to highest rank.

private void sort(PlayingCard[] hand)
    {
        PlayingCard temp;
        for(int i = 0; i < hand.length ; i++) 
        {
            
           for(int j = i+1; j < hand.length ; j++)
           {
               int compareRank = hand[j].getRank().compareTo(hand[i].getRank());
               
               if(compareRank < 0){
                   temp = hand[i];
                   hand[i] = hand[j];
                   hand[j] = temp;
               }
           }
            
        }
    }