|
![]() |
Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming > Application Programming | |
Java: sort parallel arrays
|
||
![]() |
|
|
LinkBack | Thread Tools |
|
|
#1 (permalink) | ||||||||||||
|
4.0 GHz
|
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};
}
__________________
COD4: Nuxes Steam: Nuxes Age of Conan: Nuxes, lvl 50 Aquilonian Guardian, Bane server (cultural PVP) Help Domo Kun achieve world domination!
Last edited by Nuxes : 05-08-08 at 01:12 PM. |
||||||||||||
|
|
|
|
#2 (permalink) | |||||||||||||
|
PC Gamer
|
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"╩╪Unofficial Lego Lovers Club╩╪ GRID Drift Club
|
|||||||||||||
|
|
|
|
#3 (permalink) | ||||||||||||
|
4.0 GHz
|
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!
|
||||||||||||
|
|
|
|
#4 (permalink) | |||||||||
|
Overclocker
|
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]);
}
|
|||||||||
|
|
|
|
|
#5 (permalink) | ||||||||||||
|
4.0 GHz
|
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!
|
||||||||||||
|
|
|
|
#6 (permalink) | |||||||||||||
|
Kernel Sanders
|
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;
}
}
|
|||||||||||||
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
|
|