Results 1 to 11 of 11
Thread: JAVA problem array sorting
- 09-28-2012, 06:53 AM #1
Member
- Join Date
- Sep 2012
- Posts
- 5
- Rep Power
- 0
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 ?
- 09-28-2012, 09:13 AM #2
Re: JAVA problem array sorting
Why do they call it rush hour when nothing moves? - Robin Williams
- 09-28-2012, 09:39 AM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,408
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 09-28-2012, 10:09 AM #4
Re: JAVA problem array sorting
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).
- 09-28-2012, 12:36 PM #5
Member
- Join Date
- Sep 2012
- Posts
- 5
- Rep Power
- 0
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?
- 09-28-2012, 12:48 PM #6
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.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 09-28-2012, 01:00 PM #7
Member
- Join Date
- Sep 2012
- Posts
- 5
- Rep Power
- 0
- 09-28-2012, 04:31 PM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,408
- Blog Entries
- 7
- Rep Power
- 17
Re: JAVA problem array sorting
You obviously didn't read my blog article (also see reply #3).
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 09-29-2012, 08:59 AM #9
Member
- Join Date
- Sep 2012
- Posts
- 5
- Rep Power
- 0
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.
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.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)); } }
//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 XDLast edited by panter001nx; 09-29-2012 at 09:34 AM.
- 09-30-2012, 07:32 AM #10
Member
- Join Date
- Sep 2012
- Posts
- 5
- Rep Power
- 0
Re: JAVA problem array sorting
can anyone help me please?
- 09-30-2012, 08:04 AM #11
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,408
- Blog Entries
- 7
- Rep Power
- 17
Re: JAVA problem array sorting
When people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
Help with 2d array sorting problem
By broo7198 in forum New To JavaReplies: 9Last Post: 09-30-2011, 10:39 PM -
Sorting Array UI
By Brandon Seale in forum New To JavaReplies: 6Last Post: 02-18-2011, 01:50 AM -
Java Sorting Array of points!
By Jcbconway in forum Advanced JavaReplies: 26Last Post: 10-27-2010, 04:07 AM -
Help with java sorting two dimensional array
By Joycey in forum New To JavaReplies: 2Last Post: 03-27-2010, 02:36 AM -
Sorting Array
By saqib15 in forum New To JavaReplies: 1Last Post: 02-12-2010, 03:42 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks