Results 1 to 11 of 11
  1. #1
    panter001nx is offline Member
    Join Date
    Sep 2012
    Posts
    5
    Rep Power
    0

    Default JAVA problem array sorting

    i'm new in java programming language and i've just started.
    so here's my problem:
    i use three arrays where in

    System.out.println("Enter number of process: ");
    int process = in.nextInt();
    String name[] = new String[process];
    int age[] = new int[process];
    double income[] = new double[process];

    bla bla bla so basically i used three arrays and a for loop and the outcome is this.
    (note: this is a user input)

    so here's my inputs
    ================
    || Name || Age || Income ||
    ================
    || chuck || 40 || 40,000 ||
    || norris || 65 || 32,000 ||
    || nemo || 14 || 37,200 ||
    ================

    what should i do if i want it to sort like this?

    ================
    || Name || Age || Income ||
    ================
    || nemo || 14 || 37,200 ||
    || chuck || 40 || 40,000 ||
    || norris || 65 || 32,000 ||
    ================

    i want to sort it by age from the youngest to the oldest
    and still the name and income is still intact with the age.

    can somebody help me ?

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: JAVA problem array sorting

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,309
    Blog Entries
    7
    Rep Power
    20

    Default Re: JAVA problem array sorting

    Managing data in 'parallel' arrays is silly but if you really want to do it that way, read one of my blog articles; it describes a fast sort method (heap sort) that can do what you want.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default Re: JAVA problem array sorting

    Quote Originally Posted by panter001nx View Post
    and still the name and income is still intact with the age.
    what you try is dangerous: the elements in the three arrays are not linked together. suppose there are two elements with the same age. if you sort all arrays the income and name may be messed up. my suggestion is to create a class person with the fields name, age and income that implements the interface Comparable and then you can invoke java.util.Arrays.sort(yourArray).

  5. #5
    panter001nx is offline Member
    Join Date
    Sep 2012
    Posts
    5
    Rep Power
    0

    Default java sorting problem help :(

    i've tried to use 3 arrays which are

    System.out.println("enter number of process: ");
    int process = in.nextInt();

    String name[] = new String[process];
    int ArrivalT[] = new int[process];
    int BurstT[] = new int[process];

    and then i've used for loop for inputs of name, BT and AT.
    i've also got the Average turn around time(ATAT) wherein i sum up all the burst time and then divided it by the number of process.
    =================================
    || PROCESS || BT || AT ||
    || P1 || 10 || 2 ||
    || P2 || 6 || 1 ||
    || P3 || 8 || 0 ||
    =================================
    Average Turn Around Time: 8.00 ms
    =================================

    so my problem is i don't know how to get first the lowest AT and display it first then sort the rest by lowest to highest BT and still after i get and sorted the values, it will still be linked with each other. this is what i want to get after sorting

    =================================
    || PROCESS || BT || AT ||
    || P3 || 8 || 0 || <]===== display first the lowest AT
    || P2 || 6 || 1 || <]=====then afterwise sort the BT from lowest to highest.
    || P1 || 10 || 2 ||
    =================================

    can anybody help me? what should i do?

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: JAVA problem array sorting

    Please go through the Forum Rules -- particularly the second paragraph.

    I've merged your second thread here.

    Even when you have a different, new question, remember it's bad for m to abandon a thread without replying to the responses and doing that won't incent members to help you with later questions. Also, links posted in responses are meant for you to go through. Clearly, you didn't bother.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    panter001nx is offline Member
    Join Date
    Sep 2012
    Posts
    5
    Rep Power
    0

    Default Re: JAVA problem array sorting

    Quote Originally Posted by DarrylBurke View Post
    Please go through the Forum Rules -- particularly the second paragraph.

    I've merged your second thread here.

    Even when you have a different, new question, remember it's bad for m to abandon a thread without replying to the responses and doing that won't incent members to help you with later questions. Also, links posted in responses are meant for you to go through. Clearly, you didn't bother.

    db
    sorry sir, its my bad.
    @j2me64
    yes it became messed up and i'm pretty confused with it. i want it to be linked together so if i sorted it, the values will still be attached after being sort

  8. #8
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,309
    Blog Entries
    7
    Rep Power
    20

    Default Re: JAVA problem array sorting

    You obviously didn't read my blog article (also see reply #3).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    panter001nx is offline Member
    Join Date
    Sep 2012
    Posts
    5
    Rep Power
    0

    Default Re: JAVA problem array sorting

    @JosAH
    sorry if you felt like i ignored you. sorry i'll read it.

    @topic
    regarding on my sorting problem here's what i've done. i've sorted it through its burst time so this is what i do
    it'll be sorted from the least burst time to the highest bursttime.

    Java Code:
    import java.util.*;
    public class SHORTESTJOBFIRST_NP_FINAL{
    public static void main(String[]args)
    {
    Scanner in = new Scanner(System.in);
    System.out.print("Enter Number of Process: ");
    int process = in.nextInt();
    String name[] = new String[process];
    int BurstT[] = new int[process];
    int ArrivalT[] = new int[process];
    for(int x = 0; x<process; x++)
    {
    System.out.print("Name of process: ");
    name[x] = in.next();
    System.out.print("Enter BurstTime: ");
    BurstT[x] = in.nextInt();
    System.out.print("Enter ArrivalT: ");
    ArrivalT[x] = in.nextInt();
    }
    System.out.print("\f");
    System.out.println("**************[USER INPUTTED VALUES]**************");
    System.out.println("==================================================");
    System.out.println("||\tPROCESS\t||\tBT\t||\tAT\t||");
    System.out.println("==================================================");
    for(int x=0;x!=process;x++) {
        System.out.println("||\t" + name[x] + "\t||\t" + BurstT[x] + "\t||\t" + ArrivalT[x] + "\t||");
      }
    System.out.println("==================================================");
    int tempAT, tempBT;
    String tempname;
    boolean check = false;
    
      do{
        check=false;
          for (int i=0;i < (process-1); i++){
           if (BurstT[i] > BurstT[i+1]){
             tempAT=ArrivalT[i];
             ArrivalT[i]=ArrivalT[i+1];
             ArrivalT[i+1]=tempAT;
    
             tempBT=BurstT[i];
             BurstT[i]=BurstT[i+1];
             BurstT[i+1]=tempBT;
             
             tempname= name[i];
             name[i]= name[i+1];
             name[i+1]=tempname;
    
             check=true;
             }
          }
      }while(check);
    System.out.println("*********[AFTER SORTING INPUTTED VALUES]**********");
    System.out.println("==================================================");
    System.out.println("||\tPROCESS\t||\tBT\t||\tAT\t||");
    System.out.println("==================================================");
    for(int x=0;x!=process;x++) {
        System.out.println("||\t" + name[x] + "\t||\t" + BurstT[x] + "\t||\t" + ArrivalT[x] + "\t||");
      }
    System.out.println("==================================================");
    System.out.println();
    double sumBT = 0;
    //nevermind this computing for the waiting time coz i'm still not finished in this
    System.out.println("Computing for Waiting Time: ");
    for(int x=0;x!=process;x++)
    {
    System.out.println(name[x]+" = "+BurstT[x]+" - "+ArrivalT[x]);
    }
    System.out.println("Turn Around Time(TAT) for each processes: ");
    for(int x=0;x!=process;x++)
    {
    System.out.println(name[x]+" = "+BurstT[x]);
    }
    
    
    for(int x=0;x!=process;x++)
    {
    sumBT +=BurstT[x];
    }
    System.out.printf("Average Turn Around Time(ATAT): %5.2f ms",(sumBT/process));
    }
    }
    now what i nid it to do is get 1 process that has the very least arrival time because that's where the process will start and then display it on the top, and then i'll sort the rest by regarding on their BurstTime from lowest to highest.
    //sorry i'm not good in explaining so i'll just visualize what i want to say.

    i'll say if i input this:
    **************[USER INPUTTED VALUES]**************
    ==================================================
    || PROCESS || BT || AT ||
    ==================================================
    || p1 || 8 || 4 ||
    || p2 || 10 || 1 ||
    || p3 || 2 || 2 ||
    || p4 || 7 || 3 ||
    || p5 || 4 || 0 ||
    ==================================================

    this will be the result but then
    *********[AFTER SORTING INPUTTED VALUES]**********
    ==================================================
    || PROCESS || BT || AT ||
    ==================================================
    || p3 || 2 || 2 ||
    || p5 || 4 || 0 ||
    || p4 || 7 || 3 ||
    || p1 || 8 || 4 ||
    || p2 || 10 || 1 ||
    ==================================================

    I want it to be like this.
    ==================================================
    || PROCESS || BT || AT ||
    ==================================================
    || p5 || 4 || 0 || <==== where in first is i'll get the lowest ArrivalTime then display it on top of the sorted list
    || p3 || 2 || 2 || <==== and then the rest is sorted via their BurstTime and it'll be lowest to highest BurstTime
    || p4 || 7 || 3 ||
    || p1 || 8 || 4 ||
    || p2 || 10 || 1 ||
    ==================================================

    can anybody help me on this ? please XD
    Last edited by panter001nx; 09-29-2012 at 09:34 AM.

  10. #10
    panter001nx is offline Member
    Join Date
    Sep 2012
    Posts
    5
    Rep Power
    0

    Default Re: JAVA problem array sorting

    can anyone help me please?

  11. #11
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,309
    Blog Entries
    7
    Rep Power
    20

    Default Re: JAVA problem array sorting

    Quote Originally Posted by panter001nx View Post
    can anyone help me please?
    See my replies #3 and #8; that blog article is full of boiler plate code and text that explains it all. It would be silly to repeat it all here.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Help with 2d array sorting problem
    By broo7198 in forum New To Java
    Replies: 9
    Last Post: 09-30-2011, 10:39 PM
  2. Sorting Array UI
    By Brandon Seale in forum New To Java
    Replies: 6
    Last Post: 02-18-2011, 01:50 AM
  3. Java Sorting Array of points!
    By Jcbconway in forum Advanced Java
    Replies: 26
    Last Post: 10-27-2010, 04:07 AM
  4. Help with java sorting two dimensional array
    By Joycey in forum New To Java
    Replies: 2
    Last Post: 03-27-2010, 02:36 AM
  5. Sorting Array
    By saqib15 in forum New To Java
    Replies: 1
    Last Post: 02-12-2010, 03:42 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •