Jump to content

Selection sort not working right

GDRRiley
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define HAND_SIZE 13
#define DECK_SIZE 52
   int table[4][13];
//desk of cards
    // s 0-12 h 13-25 d 26-38 c 39-51
    //suit n/13
    //rank n%3
//shuffle
    //shuffle mvoe to temp
    //temp deck at n1
    //deck at n1 deck at n2
    //deck at n2= temp
//print out in list
    // deal right
    // top to bottom every get 13
    
//selection sort
int sort(int p){
    printf("%d\n",p);
    int pos=0;
     for (int i=0; i<12; i++) // finding minimum element (n-1) times
  {
   pos = i;
    for (int d = i + 1; d < 12; d++){
      if (table[p][pos] > table[p][i]){
         pos = d;  
      }
       
    
    if (pos != i){
      int t = table[p][i];
      table[p][i] = table[p][pos];
      table[p][pos] = t;
    }
    }
  }
  
    return 1;
}


int main(void)
{
    srand(time(NULL));
    printf("Bridge\n");
    int deck [52];
    
    char suits[]={'s','h','d','c'};
  
   //shuffle
   for(int i=0; i<DECK_SIZE; i++){
       deck[i]=i;
   }
    {
        
        for (int i = 0; i < DECK_SIZE; i++) 
        {
        int j= rand()%52;
          int temp = deck[j];
          deck[j] = deck[i];
          deck[j] = temp;
        }
    }
 
   
   //deal 13 times
   int c=0;
   int h=0;
   while( c<52){
     for(int p=0; p<4; p++){
        table[p][h]=deck[c]; 
        c++; 
     }  
     h++;
   }
   
   //sort each hand highest to lowest
   
   for(int play=0; play<4; play++){
   sort(play);
   }
   
   printf("hi\n");
   // test print
   for(int f=0; f<4; f++){
   for(int i=0; i<13; i++){
   printf("%d ",table[f][i]);
   }
   printf("\n");
   }
   
   //print out hand
   printf("\n\t\t\tS");
   printf("\n\t\t\tH");
   printf("\n\t\t\tD");
   printf("\n\t\t\tC\n\n");
   
                        
    printf("S");    printf("\t*********");    printf("\tS bob\n");
    printf("H");    printf("\t*   N   *");    printf("\tH test\n");
    printf("D");    printf("\t*W     E*");    printf("\tD hello \n");
    printf("C");    printf("\t*   S   *");    printf("\tC hi \n");
                    printf("\t*********");
    printf("\nS");
    printf("\nH");
    printf("\nD");
    printf("\nC\n");
    
      
      
    return 0;
}

my primary issues currently is I can't get my section sort working right and the deal section of code isn't working ether.

I'm new to C while I've done similar work in java I left it all on my high school computer so I can't go back to reference it.

bad bracket placement.

Edited by GDRRiley
fixed it

Good luck, Have fun, Build PC, and have a last gen console for use once a year. I should answer most of the time between 9 to 3 PST

NightHawk 3.0: R7 5700x @, B550A vision D, H105, 2x32gb Oloy 3600, Sapphire RX 6700XT  Nitro+, Corsair RM750X, 500 gb 850 evo, 2tb rocket and 5tb Toshiba x300, 2x 6TB WD Black W10 all in a 750D airflow.
GF PC: (nighthawk 2.0): R7 2700x, B450m vision D, 4x8gb Geli 2933, Strix GTX970, CX650M RGB, Obsidian 350D

Skunkworks: R5 3500U, 16gb, 500gb Adata XPG 6000 lite, Vega 8. HP probook G455R G6 Ubuntu 20. LTS

Condor (MC server): 6600K, z170m plus, 16gb corsair vengeance LPX, samsung 750 evo, EVGA BR 450.

Spirt  (NAS) ASUS Z9PR-D12, 2x E5 2620V2, 8x4gb, 24 3tb HDD. F80 800gb cache, trueNAS, 2x12disk raid Z3 stripped

PSU Tier List      Motherboard Tier List     SSD Tier List     How to get PC parts cheap    HP probook 445R G6 review

 

"Stupidity is like trying to find a limit of a constant. You are never truly smart in something, just less stupid."

Camera Gear: X-S10, 16-80 F4, 60D, 24-105 F4, 50mm F1.4, Helios44-m, 2 Cos-11D lavs

Link to comment
Share on other sites

Link to post
Share on other sites

There's two issues with your sort code, which are kind of hard to see because your brackets are all over the place and your variable names are too short. Try to use more descriptive names.

 

First, you're swapping inside the inner loop, which isn't needed. Not a bug as such, just results in many unnecessary swaps.

 

Second, your inner loop is always comparing the index of your current lowest value ("pos") against the start index ("i") instead of your current search index ("d"). Since "pos" and "i" point to the same value at this point, pos never gets updated, so you're actually never swapping anything.

 

Move the swap outside the inner loop and compare against "table[p][ d ]" instead of "table[p][ i ]" and it should start to work. Here's a cleaned up version that works (sorry, it's Java, since I wanted to be able to easily validate it inside my IDE):

private void sort(final int[][] table, final int tableIndex) {
	final int[] array = table[tableIndex];
	int indexMinValue;

	for (int startIndex = 0; startIndex < array.length; startIndex++) {
		indexMinValue = startIndex;

		for (int index = startIndex + 1; index < array.length; index++) {
			if (array[index] < array[indexMinValue]) {
				indexMinValue = index;
			}
		}

		if (indexMinValue != startIndex) {
			final int t = array[startIndex];
			array[startIndex] = array[indexMinValue];
			array[indexMinValue] = t;
		}
	}
}

@Test
public void testSort() {
	final int[][] table = {
			{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 },
			{ 0, 9, 8, 7, 6, 5, 4, 3, 2, 1 }
	};

	sort(table, 0);
	sort(table, 1);

	System.out.println("Array[0] = " + Arrays.toString(table[0]));
	System.out.println("Array[1] = " + Arrays.toString(table[1]));
}

 

~edit: There's an additional error I missed. Your loops are off by one. You should be comparing "index < 13" not "< 12". I would suggest to actually use the HAND_SIZE and DECK_SIZE constants you defined at the start ;)

Remember to either quote or @mention others, so they are notified of your reply

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×