Overclock.net - Overclocking.net
     
 
Home Gallery Reviews Blogs Register Today's Posts Mark Forums Read Members List


Go Back   Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming > Application Programming

Reply
 
LinkBack Thread Tools
Old 05-08-08   #1 (permalink)
4.0 GHz
 
Nuxes's Avatar
 
intel nvidia

Join Date: Apr 2007
Location: Arkansas
Posts: 218

Rep: 14 Nuxes Unknown
Unique Rep: 14
Trader Rating: 4
Default Java: sort parallel arrays

For my Java final project, I have to create a program that will allow a user to sort and search through a list of CDs:


I have put them into two 2-dim arrays:
Code:
String[][] first = { {"AC/DC", "Plug me In"},
                         {"Beatles", "Yellow Submarine"},
                         {"Creed", "Human Sacrifice"},
                         {"U-2", "The Joshua Tree"},
                         {"Queen", "Say It's Not True"},
                         {"Eric Clapton", "Rush"},
                         {"MatchBox 20", "Bent"},
                         {"Aero Smith", "Just Push Play"},
                         {"Chris Isaak", "Best of Chris Isaak"},
                         {"U-2", "Window in the Skies"},
                         {"Elton John", "Rocketman"},
                         {"Tool", "The Lowdown"}
                         {"Queen", "Another One Bites the Dust"},
                         {"MatchBox 20", "Matchbox 20"},
                         {"Foo Fighters", "The Pretender"}
                         {"Eric Clapton", "Tears in Heaven"};
                       }
                       
int[][] second = {{101,2},
    		      {2, 5},
                      {56, 23},
                      {60, 23},
                      {34, 7},
                      {5, 26},
                      {15, 9},
                      {42, 12},
                      {31, 11},
                      {60, 24},
                      {18, 18},
                      {13, 1}
                      {24, 8},
                      {15, 6},
                      {16, 19}
                      {22, 20};
    				 }
My problem is, how do I sort one (by artist name, bin number, etc.) array and keep the other in sync? Also, I would probably be easiest to put this in a single method, but how would I tell it how to sort?
__________________
COD4: Nuxes
Steam: Nuxes
Age of Conan: Nuxes, lvl 50 Aquilonian Guardian, Bane server (cultural PVP)

Help Domo Kun achieve world domination!

System: Compy DX10
CPU
E8500 3.16 @4.005
Motherboard
Abit IP35V
Memory
4x Patriot 1GB 800 @842Mhz
Graphics Card
EVGA GeForce 8800GTS 320MB @600/900
Hard Drive
Seagate 7200rpm 320GB SATA
Sound Card
Creative X-Fi Elite Pro
Power Supply
Antec True Power Trio 650W
Case
Antec Nine Hundred
CPU cooling
TR Big Typhoon
OS
Vista Home Premium 64-bit
Monitor
19" LG Flatron Wide

Last edited by Nuxes : 05-08-08 at 01:12 PM.
Nuxes is online now Nuxes's Gallery   Reply With Quote
Old 05-08-08   #2 (permalink)
PC Gamer
 
legoman786's Avatar
 
intel ati

Join Date: Mar 2006
Location: Tucson, AZ
Posts: 4,484
Blog Entries: 4

Rep: 161 legoman786 is acknowledged by manylegoman786 is acknowledged by many
Unique Rep: 137
Folding Team Rank: 348
Trader Rating: 7
Default

Use the [code] tags if you want to keep everything in order on the forums

I'd help you but I don't know jack about Java.
__________________
¿uʍop ǝpısdn ʇı pɐǝɹ noʎ uɐɔ
Aumotocnic "An unfortunate member of the overclock.net insomnia club"
I listen to all music, and find most of it good.

System: 9.80665 m/s²
CPU
E2140
Motherboard
Foxconn P9657AB
Memory
Mushkin DDR2-800 2x1GB (5-5-5-18 1.8v Stock)
Graphics Card
Visiontek HD3870 512
Hard Drive
WD 80GB + 'Cuda 500GB + WD 40GB
Sound Card
Realtek + ProMedia 2.1
Power Supply
Corsair VX550W
Case
Rocketfish Full Tower
CPU cooling
Stock
GPU cooling
Stock
OS
Vista Ultimate x64
Monitor
Viewsonic Graphics G220fb 21" CRT
legoman786 is offline I fold for Overclock.net legoman786's Gallery   Reply With Quote
Old 05-08-08   #3 (permalink)
4.0 GHz
 
Nuxes's Avatar
 
intel nvidia

Join Date: Apr 2007
Location: Arkansas
Posts: 218

Rep: 14 Nuxes Unknown
Unique Rep: 14
Trader Rating: 4
Default

Quote:
Originally Posted by legoman786 View Post
Use the [code] tags if you want to keep everything in order on the forums

I'd help you but I don't know jack about Java.
Ah, thank you!
__________________
COD4: Nuxes
Steam: Nuxes
Age of Conan: Nuxes, lvl 50 Aquilonian Guardian, Bane server (cultural PVP)

Help Domo Kun achieve world domination!

System: Compy DX10
CPU
E8500 3.16 @4.005
Motherboard
Abit IP35V
Memory
4x Patriot 1GB 800 @842Mhz
Graphics Card
EVGA GeForce 8800GTS 320MB @600/900
Hard Drive
Seagate 7200rpm 320GB SATA
Sound Card
Creative X-Fi Elite Pro
Power Supply
Antec True Power Trio 650W
Case
Antec Nine Hundred
CPU cooling
TR Big Typhoon
OS
Vista Home Premium 64-bit
Monitor
19" LG Flatron Wide
Nuxes is online now Nuxes's Gallery   Reply With Quote
Old 05-08-08   #4 (permalink)
Overclocker
 
Manyak's Avatar
 
intel nvidia

Join Date: Mar 2008
Posts: 2,091

Rep: 180 Manyak is acknowledged by manyManyak is acknowledged by many
Unique Rep: 130
Trader Rating: 10
Default

Check out these sorting algorithms.

The easiest one is called bubblesort. And all you need to do is use the same indexes between both arrays.

it goes something like:

Code:
var i;
var j;

for (i=0; i<array.length; i++)
    for (j=array.length; j>i+1; j--)
        if (array[j-1][0] > array[j][0])
        {
            swap(array[j-1], array[j]);
            swap(second_array[j-1], second_array[j]);
        }
__________________
Under Construction: [Project] Wraith
On Hold: [Project] Belial

System: Gravehouse
CPU
Intel E8400
Motherboard
Intel DX38BT
Memory
OCZ Platinum DDR3-1333
Graphics Card
Geforce 8800gts G80
Hard Drive
2x WD RE2 500GB RAID0
Sound Card
X-Fi Platinum
Power Supply
ABZ Tagan 800W
Case
CM Stacker 832
Manyak is offline   Reply With Quote
Old 05-08-08   #5 (permalink)
4.0 GHz
 
Nuxes's Avatar
 
intel nvidia

Join Date: Apr 2007
Location: Arkansas
Posts: 218

Rep: 14 Nuxes Unknown
Unique Rep: 14
Trader Rating: 4
Default

So would this be correct for sorting by bin number?
Code:
private static void sortarray(int[][] array){
	   
	int i,j;

	for (i=0; i<array.length; i++)
    	    for (j=array.length; j>i+1; j--)
        	if (second[0][j-1] > first[0][j])
        	{	
                 swap(second[j-1], second[j]);
                 swap(first[j-1], first[j]);
        	}
	}
__________________
COD4: Nuxes
Steam: Nuxes
Age of Conan: Nuxes, lvl 50 Aquilonian Guardian, Bane server (cultural PVP)

Help Domo Kun achieve world domination!

System: Compy DX10
CPU
E8500 3.16 @4.005
Motherboard
Abit IP35V
Memory
4x Patriot 1GB 800 @842Mhz
Graphics Card
EVGA GeForce 8800GTS 320MB @600/900
Hard Drive
Seagate 7200rpm 320GB SATA
Sound Card
Creative X-Fi Elite Pro
Power Supply
Antec True Power Trio 650W
Case
Antec Nine Hundred
CPU cooling
TR Big Typhoon
OS
Vista Home Premium 64-bit
Monitor
19" LG Flatron Wide
Nuxes is online now Nuxes's Gallery   Reply With Quote
Old 05-08-08   #6 (permalink)
Kernel Sanders
 
rabidgnome229's Avatar
 
intel nvidia

Join Date: Feb 2006
Location: Pittsburgh
Posts: 4,894
Blog Entries: 1

Rep: 548 rabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famous
Unique Rep: 326
FAQs Submitted: 6
Trader Rating: 5
Default

Instead of using 2 2D arrays you should use 4 1D arrays. The way it is coded now would make the code a bit more complicated, and storing in an nx2 matrix rather than a 2xn matrix can decrease performance by up to half.

The cleanest way to implement this is with a class for entries
Code:
public class CDEntry implements Comparable{
	public String artist, title;
	int code, bin;
	
	public CDEntry(String a, String t, int c, int b){
		artist = a; title = b;
		code = c; bin = b;
	}
	
	public CDEntry(){
		artist = title = null;
		code = bin = 0;
	}
	
	int compareTo(Object o){
		if(! (o instanceof CDEntry))
			return 1;
		CDEntry other = (CDEntry)o;
		if(other.bin > bin) return -1;
		else if(other.bin == bin) return 0;
		else return 1;
	}
}
Then you can create a single array of entries. Making it implement Comparable allows you to use the Arrays.sort(Object[] o) method to sort it using an optimized algorithm that you don't have to write yourself
__________________
BIG BROTHER
I put on my robe and wizard hat...

IS WATCHING

System: It goes to eleven
CPU
E6300
Motherboard
DS3
Memory
2GB XMS2 DDR2-800
Graphics Card
EVGA 8600GTS
Hard Drive
1.294 TB
Sound Card
Audigy 2 ZS
Power Supply
Corsair 520HX
Case
Lian-Li v1000B Plus
CPU cooling
TTBT
GPU cooling
Thermalright V2
OS
Arch Linux/XP
Monitor
Samsung 226bw
rabidgnome229 is offline Overclocked Account   Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools



All times are GMT -4. The time now is 08:38 PM.


Overclock.net is a Carbon Neutral Site Creative Commons License Internet Security By ControlScan

Terms of Service / Forum Rules | Privacy Policy | Advertising | Become an Official Vendor
Copyright © 2008 Shogun Interactive Development. Most rights reserved.
Page generated in 0.15293 seconds with 9 queries