Solved

Sorting two single dimensional arrays

Posted on 2003-12-03
6
614 Views
Last Modified: 2010-03-31
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("Unsorted movie titles:");
  for(x = 0; x < 5; ++x)
   System.out.println(inventoryId[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("Sorted movie titles:");
   System.out.println(inventoryId[x] + "    " + videos[x]);
 }
}
0
Comment
Question by:angelori
  • 3
  • 2
6 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 400 total points
ID: 9868124
You need to swap both array values at the same time. I'll let you fill in the holes here:

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("Unsorted movie titles:");
  for(int i = 0;i < inventoryId.length ;i++) {
    System.out.println(inventoryId[i]);

  }
  bubblesort(videos, inventoryId);
  for(int i = 0;i < inventoryId.length ;i++) {
      System.out.print(inventoryId[i] + ", ");
      System.out.println(videos[i]);

  }
 }

public static void bubblesort(String[] videos, int[] ids)
{
int a,b, temp;
String tempTitle;

//Sort by inventoryId
  for(a = 0; a < ids.length; a++)
    for(b = 0; b < a; 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;
      }
 }
}
0
 
LVL 15

Expert Comment

by:JakobA
ID: 9869429
>> CEHJ:   I think you mean:

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
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9869469
>>I think you mean:

Do I? Why? ;-)
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 15

Expert Comment

by:JakobA
ID: 9869476
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;
      }
 }
}
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9884671
8-)

JakobA's last optimization is good btw
0
 

Author Comment

by:angelori
ID: 9884676
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.  
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
allswap challenge 6 99
Way to decrease size of apk file 9 71
Opening PDF on button click and fill new document 2 39
hibernate example using maven 12 42
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question