-1

Please help me got the code to arrange lowest to highest input values. Below is my sample code:

 System.out.print("Enter Number of Process: ");
 int p = Integer.parseInt(input.readLine());

 int [] arrival = new int[p];
 int [] size = new int[p];
 for(int i=0;i<p;i++){
 System.out.print("Arrival Time of Process #"+(i+1)+":");
 arrival[i]=Integer.parseInt(input.readLine());

The expected output must be:

Enter Number of Process: 4
Arrival Time of Process # 1: 100
Arrival Time of Process # 2: 110
Arrival Time of Process # 3: 102
Arrival Time of Process # 4: 101
P1: 100
P4: 101
P3: 103
P2: 110

If you're familiar on First Come First Serve Algorithm, that was related on this.

Alexei - check Codidact
  • 22,016
  • 16
  • 145
  • 164
JhayCee
  • 19
  • 5

2 Answers2

0

Use Arrays.sort.

int[] is = {100, 110, 101, 102};
Arrays.sort(is);
for (int i : is) {
    System.out.println(i);
}

Output:

100
101
102
110
0

I looks like you'd also like to keep track of the original indexes of the input values while doing the sorting. The thread I recommend you to read is sorting pairs of (value, index) compared by value. Please try the following approach:

import java.io.IOException;
import java.util.Arrays;
import java.io.BufferedReader;
import java.io.InputStreamReader;

public static void main(String[] args) throws IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    System.out.print("Enter Number of Process: ");
    int p = 0;

    try {
        p = Integer.parseInt(br.readLine());
    } catch (NumberFormatException nfe) {
        System.err.println("Invalid Format!");
    }

    Pair[] arrival = new Pair[p];
    for (int i = 0; i < p; i++) {
        System.out.print("Arrival Time of Process #" + (i + 1) + ":");
        try {

            arrival[i] = new Pair(i + 1, Integer.parseInt(br.readLine()));
        } catch (NumberFormatException nfe) {
            System.err.println("Invalid Format!");
        }
    }
    Arrays.sort(arrival);
    for (int i = 0; i < arrival.length; i++) {
        System.out.println("P" + arrival[i].index + ": " + arrival[i].value);
    }
}

// a little change was made to the original code
public static class Pair implements Comparable<Pair> {

    public final int index;
    public final int value;

    public Pair(int index, int value) {
        this.index = index;
        this.value = value;
    }

    @Override
    public int compareTo(Pair other) {
        return Integer.valueOf(this.value).compareTo(other.value);
    }
}

Output is:

Enter Number of Process: 4
Arrival Time of Process #1:100
Arrival Time of Process #2:110
Arrival Time of Process #3:103
Arrival Time of Process #4:101
P1: 100
P4: 101
P3: 103
P2: 110
Community
  • 1
  • 1
Quinn
  • 4,394
  • 2
  • 21
  • 19
  • Thankyou so much. This is what i need :) not familiar on your used codes but I study that functions . – JhayCee Mar 04 '16 at 07:37
  • why above code if sort(arrival)? only the value sorted? not the index? – JhayCee Mar 04 '16 at 15:24
  • @JhayCee: The new class Pair does the trick. sorting is still based on values, but the original index is kept in the Pair class as a field. – Quinn Mar 04 '16 at 16:02