Thread: Bubble sort 2 column 2D array

  1. #1
    Registered User
    Join Date
    Jan 2019
    Posts
    8

    Bubble sort 2 column 2D array

    Good afternoon,
    I was attempting to use a bubble sort to place my results of the array in order and am only able to sort the second column. For some reason, I am not able to get the rows to stay together. Any pointers would be appreciated.

    Code:
    void scores_sort(void)
    {
        int y;
        for(int i=0;i<score;i++)
        {
            for(int j=0; j<teams-i; j++)
            {
                if(game[j+1][1] < game[j][1]) // swap values
                {
                    y = game[j][1];
                    game[j][1] = game[j+1][1];
                    game[j+1][1] = y;
                }
            }
        }
        for(i=0; i<score; i++)
            
        {
            
            for(j=0;j<teams;j++){
                
                printf("%d\t", game[i][j]);
                
            }
            printf("\n");
        }
        
        return;
    }
    Last edited by jlucey; 01-27-2019 at 03:11 PM.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    When you "swap" your value you must also "swap" the other "column" in the array.

  3. #3
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    You should post your whole program (as plain text, BTW).
    Also, explain exactly what you are trying to do.

    And unless you are coding it for practice, it's not recommended to ever use a bubble sort. If you want to code an n-squared sort yourself, use a selection or insertion sort. Otherwise, use the standard library qsort.
    Last edited by john.c; 01-27-2019 at 03:33 PM.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  4. #4
    Registered User
    Join Date
    Jan 2019
    Posts
    8
    john.c,
    I appreciate the recommendation, as part of our class, we must use bubble sort for this activity. As for posting the entire program, I spent most of my day yesterday trying to understand the best methods for using this forum. There is actually a permanent help post that states when posting code to always use the . I have seen some ask for all code involved, and others get annoyed and request only the code in question. I was able to actually get this to work based on the prior suggestion so once again I am grateful for the help given. I will say that both times I have posted here I have felt like I am annoying people, as a beginner just trying to understand and wrap my head around c, I do not think this forum is for me. I think this will be my last post, I will either sort things out on my own no matter how long it takes or seek help elsewhere.

  5. #5
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    You do seem rather touchy. But I was not in any way "annoyed" at you.
    I meant that if jimblumberg's suggestion didn't solve your problem then you should post the whole program.
    I noticed for instance that you are using global variables.
    I thought we could help you with that, for example.
    It is often the case that bad teachers will recommend bubble sort.
    I just meant that if you had a choice, you shouldn't use it.
    But if your feelings are so easily hurt, then you should probably not post anything anywhere.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  6. #6
    Registered User
    Join Date
    Jan 2019
    Posts
    8
    I am certainly not here to make enemies, actually, it is quite the opposite. I thought full heartedly that my question and lack of knowledge was annoying you. As I stated, I felt the same way yesterday when I asked a separate question. I found this forum and had hoped to make a few friends along the way that knew more than I, and could guide me to obtain a higher knowledge as I progress towards my degree. So no my feelings are not hurt, in any way. I was thankful legitimately for your suggestion as we have not earned or heard n-squared sort or standard library qsort. so you have given me a few items to go read on and learn. I am not here for a free ride, I know that it is my responsibility to do the work. In the 6 yrs I was in the army and 47 yrs I have been alive, I have worked hard to get to today. I did not do it alone, I had people around me that helped and supported me, but in this quest my options are limited. I hope you will forgive me for the longwinded reply and for the confusion.

  7. #7
    Registered User
    Join Date
    Jan 2019
    Posts
    8
    jimblumbergThank you for giving me the hint, I went back and looked at it and was able to sort out what I had missed. I am now sorting and the rows do stay intact as I needed.

  8. #8
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    Quote Originally Posted by jlucey View Post
    I thought full heartedly that my question and lack of knowledge was annoying you. As I stated, I felt the same way yesterday when I asked a separate question.
    I don't know what you are talking about.
    I've never heard of you before and certainly didn't respond to a question of yours before.
    However, since you are overly touchy I've added you to my ignore list and promise never to respond to you again.
    I hope that is satisfactory for you.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  9. #9
    Registered User
    Join Date
    Jan 2019
    Posts
    8
    john.c Wait no, the question was answered by another person not you. I really have made a mess of this somehow, I really did not mean for it to go sideways like this. I was trying to apologize. I am regretful that this has gone this way.

  10. #10
    Guest
    Guest
    jlucey, a few users here actually are difficult, so don't apologize.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with bubble sort 2D array
    By khoavo123 in forum C Programming
    Replies: 6
    Last Post: 02-14-2012, 11:23 PM
  2. bubble sort with an array
    By commandercool in forum C++ Programming
    Replies: 3
    Last Post: 05-13-2005, 01:29 AM
  3. Array/Structure Bubble Sort
    By JKI in forum C++ Programming
    Replies: 3
    Last Post: 09-29-2003, 11:59 AM
  4. bubble sort array of strings
    By the_head in forum C Programming
    Replies: 1
    Last Post: 04-02-2002, 05:15 PM

Tags for this Thread