angelori
asked on
Sorting two single dimensional arrays
I am having trouble sorting 2 arrays. I must use single-dimensional arrays. All I need to do is create 2 arrays - one for video titles and one for the inventory ID for each video title, sort by the inventory ID and print out the sorted list. I am totally lost and think I really messed up my code now. I had the program working up until the most important part (of course) - sorting and displaying the sorted video titles. I know you can sort by object array, but can it be done the way I am trying?? My code is below:
public class Video
{
public static void main(String[] args)
{
String[] videos = {"Lion King", "Holes", "Finding Nemo", "Beauty and the Beast", "Parent Trap"};
int[] inventoryId = {102, 101, 104, 103, 100};
int x;
System.out.println("Unsort ed movie titles:");
for(x = 0; x < 5; ++x)
System.out.println(invento ryId[x] + " " + videos[x]);
public static void bubblesort(videos, int len)
{
int a,b;
Video temp;
int lenMinusOne = len - 1;
//Sort by inventoryId
for(a = 0; a < lenMinusOne; ++a)
for(b = 0; b < lenMinusOne; ++b)
if(array[b].inventoryId > array[b + 1].inventoryId)
{
temp = array[b];
array[b] = array[b + 1];
array[b + 1] = temp;
}
for(x = 0; x < 5; ++x)
//System.out.println("Sort ed movie titles:");
System.out.println(invento ryId[x] + " " + videos[x]);
}
}
public class Video
{
public static void main(String[] args)
{
String[] videos = {"Lion King", "Holes", "Finding Nemo", "Beauty and the Beast", "Parent Trap"};
int[] inventoryId = {102, 101, 104, 103, 100};
int x;
System.out.println("Unsort
for(x = 0; x < 5; ++x)
System.out.println(invento
public static void bubblesort(videos, int len)
{
int a,b;
Video temp;
int lenMinusOne = len - 1;
//Sort by inventoryId
for(a = 0; a < lenMinusOne; ++a)
for(b = 0; b < lenMinusOne; ++b)
if(array[b].inventoryId > array[b + 1].inventoryId)
{
temp = array[b];
array[b] = array[b + 1];
array[b + 1] = temp;
}
for(x = 0; x < 5; ++x)
//System.out.println("Sort
System.out.println(invento
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>>I think you mean:
Do I? Why? ;-)
Do I? Why? ;-)
actually we can cut the sorting time in half if we think a bit. notice how the inner loop alwat push the biggest value to the end of the array. no need to sort that value again:
public static void bubblesort(String[] videos, int[] ids) // boyh arrays to be sorted
{
int a,b, temp;
String tempTitle;
int lenMinusOne = videos.length -1; // get length of arrats (minus one)
//Sort by inventoryId
for(a = lenMinusOne; a>0 ; a--) // a goes 'backwards'
for(b = 0; b < a; b++) // b only sort up to a in each scan
if(ids[a] < ids[b])
{
tempTitle = videos[b];
videos[b] = videos[a];
videos[a] = tempTitle;
temp = ids[b];
ids[b] = ids[a];
ids[a] = temp;
}
}
}
public static void bubblesort(String[] videos, int[] ids) // boyh arrays to be sorted
{
int a,b, temp;
String tempTitle;
int lenMinusOne = videos.length -1; // get length of arrats (minus one)
//Sort by inventoryId
for(a = lenMinusOne; a>0 ; a--) // a goes 'backwards'
for(b = 0; b < a; b++) // b only sort up to a in each scan
if(ids[a] < ids[b])
{
tempTitle = videos[b];
videos[b] = videos[a];
videos[a] = tempTitle;
temp = ids[b];
ids[b] = ids[a];
ids[a] = temp;
}
}
}
8-)
JakobA's last optimization is good btw
JakobA's last optimization is good btw
ASKER
Wow! Thanks to both of you! I consulted someone who is a Java programmer where I work and they were stumped too. You 2 are awesome....Sorry about the length of time in accpeting the answer, etc. This is my first time using this service.
public static void bubblesort(String[] videos, int[] ids) // boyh arrays to be sorted
{
int a,b, temp;
String tempTitle;
int lenMinusOne = videos.length -1; // get length of arrats (minus one)
//Sort by inventoryId
for(a = 0; a < lenMinusOne; a++)
for(b = 0; b < lenMinusOne; b++)
if(ids[a] < ids[b])
{
tempTitle = videos[b];
videos[b] = videos[a];
videos[a] = tempTitle;
temp = ids[b];
ids[b] = ids[a];
ids[a] = temp;
}
}
}
regards JakobA