import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.text.ParseException;
import java.util.*;
public class SJF {
public static void readFromFile() throws IOException {
BufferedReader bufReader = new BufferedReader(new FileReader("processes.txt"));
ArrayList<String> listOfLines = new ArrayList<>();
String line = bufReader.readLine();
while (line != null) {
listOfLines.add(line);
line = bufReader.readLine();
}
bufReader.close();
System.out.println("Content of ArrayLiList:");
// split by new line
int num = 0;
for (String line1 : listOfLines) {
String line2[] = line1.split(",");
// int burstTime = Integer.parseInt(line2[3].trim());
// String retrival = listOfLines.get(0);
System.out.println(" <process ID>, <arrival time>, <priority>, <CPU burst>");
System.out.println(line1);
System.out.println("At time <" + Integer.parseInt(line2[1].trim()) + "> ms, CPU starts running process < " + Integer.parseInt(line2[3].trim()) + "ID>,");
//System.out.println(burstTime);
}
// int num =0;
// for (int i = 0; i < listOfLines.size(); i++) {
// String retrival = listOfLines.get(num), line2[] = retrival.split(",");
// int num1 = Integer.parseInt(line2[3].trim());
// String retrival1 = listOfLines.get(num), line3[] = retrival1.split(",");
// int num2 = Integer.parseInt(line3[3].trim());
// if (num1 < num2) {
// //System.out.println("You got it :)!!!!!!");
// Collections.swap(listOfLines, num + 1, num + 2);
// }
//
// System.out.println(listOfLines.get(i));
// }
// System.out.println(retrival);
// System.out.println(retrival1);
}
void sort(int arr[]) {
int n = arr.length;
// One by one move boundary of unsorted subarray
for (int i = 0; i < n - 1; i++) {
// Find the minimum element in unsorted array
int min_idx = i;
for (int j = i + 1; j < n; j++)
if (arr[j] < arr[min_idx])
min_idx = j;
// Swap the found minimum element with the first
// element
int temp = arr[min_idx];
arr[min_idx] = arr[i];
arr[i] = temp;
}
}
// Driver code
public static void main(String[] args) throws ParseException, IOException {
// process id's
readFromFile();
}
}
Can someone sort the output from that Arraylist in ascending order according to the <CPU burst>
OUTPUT
Content of ArrayLiList:
<process ID>, <arrival time>, <priority>, <CPU burst>
P3, 5, 8, 20
At time <5> ms, CPU starts running process < 20ID>,
<process ID>, <arrival time>, <priority>, <CPU burst>
P4, 9, 3, 50
At time <9> ms, CPU starts running process < 50ID>,
<process ID>, <arrival time>, <priority>, <CPU burst>
P1, 9, 7, 35
At time <9> ms, CPU starts running process < 35ID>,
<process ID>, <arrival time>, <priority>, <CPU burst>
P5, 13, 9, 20
At time <13> ms, CPU starts running process < 20ID>,
<process ID>, <arrival time>, <priority>, <CPU burst>
P6, 23, 4, 50
At time <23> ms, CPU starts running process < 50ID>,
<process ID>, <arrival time>, <priority>, <CPU burst>
P7, 25, 1, 60
At time <25> ms, CPU starts running process < 60ID>,
hey,try this...
the code sorts an arrayList based on the CPU burst time
the sorting algorithm is same used by you
after the code is executed you get the arrayList "listOfLines" sorted in ascending order
as well as an integer array "arr" having sorted burst times.
Again copy in any ide to test and get better grasp of the code..
Hope this helps.Good luck!
import java.io.*;
import java.text.ParseException;
import java.util.*;
class example {
public static void main (String[] args) {
ArrayList<String> listOfLines = new
ArrayList<>();
listOfLines.add("P1, 5, 8, 40");
listOfLines.add("P1, 5, 8, 70");
listOfLines.add("P1, 5, 8, 20");
listOfLines.add("P1, 5, 8, 30");
listOfLines.add("P1, 5, 8, 90");
listOfLines.add("P1, 5, 8, 55");
int len=listOfLines.size();
int arr[]=new int[len];
for(int i=0;i<len;i++){
String line1=listOfLines.get(i);
String line2[] = line1.split(",");
arr[i] = Integer.parseInt(line2[3].trim());
}
//Sorting the arrayList using the integer values in array
for(int i=0;i<len;i++){
int min_index=i;
for(int j=i+1;j<len;j++){
if(arr[min_index]>arr[j]){
min_index=j;
}
}
if(min_index!=i){
int temp=arr[i];
arr[i]=arr[min_index];
arr[min_index]=temp;
//swapping the elements of the arrayList
String temp1=listOfLines.get(i);
listOfLines.set(i,listOfLines.get(min_index));
listOfLines.set(min_index,temp1);
}
}
for (int i=0;i<len;i++ ){
System.out.println(listOfLines.get(i)+"
"+arr[i]);
}
}
}
Get Answers For Free
Most questions answered within 1 hours.