having problem on creating this shortest job first nonpreemptive algorithm

• September 28th, 2012, 06:16 AM
morphling18
having problem on creating this shortest job first nonpreemptive algorithm
hello there i'm new in java programming and i'm trying to create a scheduling algorithm which is shortest job first nonpreemptive.

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?
• September 28th, 2012, 07:50 AM
Norm
Re: having problem on creating this shortest job first nonpreemptive algorithm
Do you have to use parallel arrays to hold the data or can you create a class to hold the data, put instances of the class in an array and sort them?
• September 28th, 2012, 08:52 AM
morphling18
Re: having problem on creating this shortest job first nonpreemptive algorithm
i can use other but i'm clueless on what i should do and i need some help.
• September 28th, 2012, 08:58 AM
Norm
Re: having problem on creating this shortest job first nonpreemptive algorithm
If the data is in a class, and instances of the class are in an array
what do you want to do with the contents of the array?
Something like:
Select one item from the array based on criteria one.
Sort the remainder of the items in the array based on criteria two.
• September 28th, 2012, 09:08 AM
morphling18
Re: having problem on creating this shortest job first nonpreemptive algorithm
i want it to be something like this.
it'll contain a string, integer, integer.

something like this
name-arrivaltime-burst time
firstprocess 8 2
secondprocess 3 4
thirdprocess 5 6
fourthprocess 7 10

something like that. it'll contain 3 values.
• September 28th, 2012, 09:19 AM
morphling18
Re: having problem on creating this shortest job first nonpreemptive algorithm
and then i'll sort it depending on it's arrival time or burst time. but still after sorting it, it'll still contain the 3 values wherein string,integer,integer.
example is:
name-arrivaltime-burst time
firstprocess 8 2
secondprocess 3 4
thirdprocess 5 6
fourthprocess 7 10

then i will sort it depending on its arrivaltime. so it'll become like this:
name - arrivaltime - bursttime
secondprocess 3 4
thirdprocess 5 6
fourthprocess 7 10
firstprocess 8 2
• September 28th, 2012, 09:30 AM
Norm
Re: having problem on creating this shortest job first nonpreemptive algorithm
Ok, now define the class, create a list of instances of the class in a container and look at finding the instance of the class with the first criteria.
• September 29th, 2012, 03:22 AM
morphling18
Re: having problem on creating this shortest job first nonpreemptive algorithm
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.

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 somebody help me ? please?
• September 29th, 2012, 08:12 AM
Norm
Re: having problem on creating this shortest job first nonpreemptive algorithm
Please edit your post and format the code with proper indentations. The source statements should not all start in the first column. Unformatted code is hard to read and understand.

Quote:

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.
After sorting on BT you need to find the lowest AT and put that at the front.
• September 29th, 2012, 01:45 PM
morphling18
Re: having problem on creating this shortest job first nonpreemptive algorithm
@Norm
what should i use to get that? any advice sir, or any recommendation on what i should use to it.
• September 29th, 2012, 01:57 PM
Norm
Re: having problem on creating this shortest job first nonpreemptive algorithm
Are you having a problem to search through an array for the lowest value in the array?
One technique is to assume the first item in the array is the smallest and then to compare all the other elements against that one to find a smaller one. When a smaller one is found, use it to compare against the rest of the elements looking for a smaller one. Continue to the end of the array.