Results 1 to 3 of 3
Thread: Bubble Sort For 2D Arrays
- 11-30-2011, 09:20 PM #1
Member
- Join Date
- Nov 2011
- Posts
- 2
- Rep Power
- 0
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"); }
- 11-30-2011, 09:33 PM #2
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.
- 11-30-2011, 09:40 PM #3
Member
- Join Date
- Nov 2011
- Posts
- 2
- Rep Power
- 0
Similar Threads
-
Question with bubble sort
By Metastar in forum New To JavaReplies: 22Last Post: 09-13-2010, 06:25 AM -
Bubble sort
By pineapple in forum New To JavaReplies: 3Last Post: 04-25-2009, 12:45 AM -
Trouble w/ Bubble Sort
By bri1547 in forum New To JavaReplies: 4Last Post: 08-01-2008, 04:41 PM -
How to sort a list using Bubble sort algorithm
By Java Tip in forum AlgorithmsReplies: 3Last Post: 04-29-2008, 08:04 PM -
need help with bubble sort
By lowpro in forum New To JavaReplies: 3Last Post: 12-17-2007, 05:27 PM
Bookmarks