Results 1 to 3 of 3
  1. #1
    tmantonym is offline Member
    Join Date
    Nov 2011
    Posts
    2
    Rep Power
    0

    Post Bubble Sort For 2D Arrays

    Hi everyone, I'm new to java programming, and was trying to make some sort of leaderboard system. I'm having a problem sorting the elements by their 2nd dimensions. I'm trying to use a bubble sort algorithm, but for some reason all players end up ranked as no.1 after going through my attempt at a bubble sort. There are 1000 players.

    Can anyone help? This is just to let you know how I'm trying to sort things:
    • [][3] is the player score. (What change is based on)
    • [][4] is the player rank. (What I want to change)


    Java Code:
            boolean flag = true;
    	    String temp;
    
    	    while (flag)
    	    {
    	    	flag = false;
    	        for (int j = 0; j < Player.player.length - 1; j++)
    	        {
    	        	System.out.println(""+Player.player[j][3]+" "+Player.player[j+1][3]);
    	        	double currentScore = Double.parseDouble(Player.player[j][3]);
    	        	double nextScore = Double.parseDouble(Player.player[j+1][3]);
    	        	if (currentScore < nextScore && currentScore != 0)    // '>' for ascending sort
    	            {
    	              	temp = Player.player[j][4];
    	                Player.player[j][4] = Player.player[j+1][4];
    	                Player.player[j+1][4] = temp;
    	                flag = true;
    	                System.out.println("A swap occured.");
    	            }
    	        	else
    	        	{
    	        		System.out.println("A swap didn't occur.");
    	        	}
    	        	System.out.println(""+ Player.player.length);
    	        	System.out.println(""+Integer.parseInt(Player.player[j][3])+" "+ Integer.parseInt(Player.player[j+1][3]));
    	        } 
    	        System.out.println("New algorithm finished");
    	    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,403
    Rep Power
    25

    Default Re: Bubble Sort For 2D Arrays

    Can you post the output from the program that shows what it does and add some comments to explain what is wrong with the output. Write a small test program with a 2 dim array for input to your method. Print out the before and after contents of the array.
    You can use the Arrays toString method to print out the second dimension. Print the first yourself.

  3. #3
    tmantonym is offline Member
    Join Date
    Nov 2011
    Posts
    2
    Rep Power
    0

    Default Re: Bubble Sort For 2D Arrays

    Okay, I won't be able to do this tonight (it's 20:40 here in England) so I'll provide all this tomorrow. Thanks for the fast reply!

Similar Threads

  1. Question with bubble sort
    By Metastar in forum New To Java
    Replies: 22
    Last Post: 09-13-2010, 06:25 AM
  2. Bubble sort
    By pineapple in forum New To Java
    Replies: 3
    Last Post: 04-25-2009, 12:45 AM
  3. Trouble w/ Bubble Sort
    By bri1547 in forum New To Java
    Replies: 4
    Last Post: 08-01-2008, 04:41 PM
  4. How to sort a list using Bubble sort algorithm
    By Java Tip in forum Algorithms
    Replies: 3
    Last Post: 04-29-2008, 08:04 PM
  5. need help with bubble sort
    By lowpro in forum New To Java
    Replies: 3
    Last Post: 12-17-2007, 05:27 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •