Solved

sort items in an array

Posted on 2003-12-07
5
266 Views
Last Modified: 2010-03-31
hi all.
I have an array , and I want to sort 3 items in that array(known values) in accending order without disterbing the others.for example if my array is
 { 1, 2, 20, 90, 30, 130, 10, 190 ,52 }
I want to sort three items contain 10, 20, 30 so the output would be
{ 1, 2, 10, 90, 20, 130, 30, 190 ,52 }. It sounds easy but I couldn't get it right.This is what I have done
 public static void main(String[] args)
    {
        int[]num = { 1, 2, 20, 90, 30, 130, 10, 190 ,52 };
        int temp;
        for(int i =0; i<num.length; i++)
        {
              if (num[i] == 10)
              {
                    temp = num[i];
                    for (int j=0; j<num.length; j++)
                    {
                          if (num[j] == 20)
                          {
                                num[i] = num[j];
                          
                                num[j] = temp;
                           }
                          if (i == j)
                          num[i] = num[j];
                    }
              }
        }
        for (int i =0; i<num.length; i++)
        System.out.print (num[i] + " ");                        
    }  
}It doesn't do any thing good
Pease , anyone can show me a good way to dothis.
thank
0
Comment
Question by:vnhenry
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 9892087
Something like:

  static void sort() {
    int[] num = { 1, 2, 20, 90, 30, 130, 10, 190 ,52 };
    int firstN = 3; //sort first n items
    int temp;
    for(int i = 0; i < num.length; i++) {
      for(int j = 0;j < firstN;j++) {
        if (num[i] < num[j]) {
          temp = num[j];
          num[j] = num[i];
          num[i] = temp;
        }
      }
    }
    for(int i = 0;i < num.length;i++) {
      System.out.println(num[i]);
    }
  }
0
 

Author Comment

by:vnhenry
ID: 9892373
I dont really get it CEHJ.
what I want to do here is given 3 items in the array which their value is 10 20 and 30.
I dont know index of there 3 numbers.
I need  to swap there 3 item so they will be sorted in order 10 befor 20 befor 30 without messing up these other items in the array
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 50 total points
ID: 9892435
Sorry - thought you wanted just the first 3 sorted. Not particularly pretty, but works:

 static void sort() {
    int[] num = {1, 2, 20, 90, 30, 130, 10, 190, 52};
    //sort first n items
    int temp;
    for (int i = num.length - 1; i > 0; i--) {
      for (int j = 0; j < i; j++) {
        if ((num[i] == 10 || num[i] == 20 || num[i] == 30) && (num[j] == 10 || num[j] == 20 || num[j] == 30)) {
          if (num[i] < num[j]) {
            temp = num[j];
            num[j] = num[i];
            num[i] = temp;
          }
        }
      }
    }
    for (int i = 0; i < num.length; i++) {
      System.out.println(num[i]);
    }
  }
0
 

Author Comment

by:vnhenry
ID: 9892554
Unbelievable!
 I Wish I have some of your talent, CEHJ
Thank you so much.

Best regard.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9892614
8-)

You could parameterize the target numbers by passing an array of target int, creating a Set of Integer and calling another function like

boolean isTargetNumber() {}
0

Featured Post

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
IBM TS2900 (3572) Tape Autoloader Java? 12 99
Java array 10 84
maximize the sum of fractions 33 70
Pass multiple values or string arrays in java as a parameter 3 72
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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:
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

738 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