The if statement in my program stopped working for some unknown reason

So I have been trying to make a program to solve process scheduling problems in java but an if statement isn't working and is in the switch statement case 2 in the below code.

What is expected to happen is when case 2 of switch statement is called the if statement has to sort the arrays in ascending order. But if the if statement isn't executing at all as if getting ignored. the same if statement was in the method GTable() before and doesn't work anymore.

Any help please?

import java.util.Scanner;
class OS
{
 public static void GTable(int [] gnt, String [] Processes, int n)
 {
  System.out.println();
  System.out.println("Gant Table:-");
  for(int i=0;i<=n-1;i++)
  { 
   System.out.print("| "+Processes[i]+" ");
  }
  System.out.println("|");
  System.out.print("0    ");
  for(int i=0;i<=n-1;i++)
  {
   System.out.print(gnt[i]+"   ");
  }
  System.out.println("\n");  
 } 
 public static int[] GT_calc(int [] BT, int [] CT, int n)
 {
  int gant=0;
  for(int i=0;i<=n-1;i++)
  { 
   gant=gant+BT[i];
   CT[i]=CT[i]+gant;
  }
 return CT;
 }
 public static void main(String args[])
 {
  int n,sw;
  int Flag=0;
  float Total_TAT=0,Total_WT=0,avg_TAT=0,avg_WT=0;
  OS o=new OS();
  Scanner s=new Scanner(System.in);
  System.out.println("Enter the number of processes: ");
  n=s.nextInt();
  String [] Processes=new String[n];
  int [] CT=new int[n];
  int [] TAT=new int[n];
  int [] WT= new int[n];
  int [] gnt=new int[n];
  System.out.println("Enter the processes ordered according to arrival time:");
  for(int i=0;i<=n-1;i++)
  {
   Processes[i]=s.next();
  }
  int [] Burst_Times=new int[n];
  System.out.println("Enter the Burst Times:");
  for(int i=0;i<=n-1;i++)
  {
   Burst_Times[i]=s.nextInt();
  }
  int BT[]=Burst_Times.clone();
  System.out.println("Enter the type of scheduling to be used(number):-");
  System.out.println("1. First In First Out Scheduling\n2. Shortest Job First Scheduling\n3. Priority Scheduling\n4. Round Robin Scheduling");
  sw=s.nextInt();
  switch(sw)
  {
   case 1:
    System.out.println("First In First Out Scheduling:-");
    o.GT_calc(BT, CT, n);
    o.GTable(gnt, Processes, n);
    break;
   case 2:
    System.out.println("Shortest Job First Scheduling:-");
    o.GT_calc(BT, CT, n);
    int temp;
    String temp1;
    for(int i=1;i<n-1;i++)
    {
     for(int j=i+1;j<n-1;j++)
     {
          if(Burst_Times[i]>Burst_Times[j]) 
          { 
       temp=BT[i];
           BT[i]=BT[j];
           BT[j]=temp;
       temp1=Processes[i];
       Processes[i]=Processes[j];
       Processes[j]=temp1;
       temp=CT[i];
       CT[i]=CT[j];
       CT[j]=temp;
      }   
     }
    }
    for(int i=0;i<=n-1;i++)
    {
     gnt[i]=gnt[i]+CT[i];
    }
        o.GTable(gnt, Processes, n);
    break;
   case 3:
    System.out.println("Coming Soon ;)");
    break;
   case 4:
    System.out.println("Coming Soon ;)");
    break;
    
  }
  for(int i=0;i<=n-1;i++)
  {
   TAT[i]=CT[i]-i;
   WT[i]=TAT[i]-Burst_Times[i];
   Total_TAT=Total_TAT+TAT[i];
   Total_WT=Total_WT+WT[i];
  }
  int ttat=(int)Total_TAT;
  int twt=(int)Total_WT;
  System.out.println("Arival Time    Burst Time    Completion Time    Turn Around Time    Wating Time");
  for(int i=0;i<=n-1;i++)
  {
   System.out.println("    "+i+"    \t"+Burst_Times[i]+"\t\t"+CT[i]+"\t\t\t"+TAT[i]+"   \t\t"+WT[i]);
  }
  System.out.println("Total Turn around Time is: "+ttat);
  System.out.println("Total Waiting Time is: "+twt);
  avg_TAT=Total_TAT/n;
  avg_WT=Total_WT/n;
  System.out.println("Average Turn around Time is: "+avg_TAT);  
  System.out.println("Average Waiting Time is: "+avg_WT);
 }
}

The output screen image has been linked here


To sort an array you must compare the values to be sorted. Your current code decides how to sort by looking at the wrong values and therefore cannot sort the arrays.

From my comment:

The condition in the if statement that decides on swapping elements depends on the Burst_Times array, but the actual swapping happens on the BT array (in this answer I left out the processes and the CT arrays because for the explanation they are not needed.)

After the first swap operation the Burst_Times and the BT array have diverged, making the complete sorting process unreliable.

Lets show this with a simple example (I named the arrays a and b for convenience and extracted the sort into a method):

public static void sort() {
    int[] a = { 1, 7, 2, 4, 5 };
    int[] b = a.clone();
    int n = a.length;
    for (int i = 1; i < n - 1; i++) {
        for (int j = i + 1; j < n - 1; j++) {
            if (a[i] > a[j]) {
                int temp = b[i];
                b[i] = b[j];
                b[j] = temp;
            }
        }
    }
    System.out.println(Arrays.toString(a));
    System.out.println(Arrays.toString(b));
}

When the if statement is reached the first time, we have i=1, j=2 and

a: 1, 7, 2, 4, 5
b: 1, 7, 2, 4, 5

Since a[i] > a[j] which is a[1] > a[2] or 7 > 2 is true, the corresponding elements in b are swapped:

a: 1, 7, 2, 4, 5
b: 1, 2, 7, 4, 5

The inner loop executes once again (with i=1, j=3) and now a[i] > a[j] is a[1] > a[3] or 7 > 4 (since the element a[1] was not touched) which is also true. Therefore b[1] and b[3] are swapped:

a: 1, 7, 2, 4, 5
b: 1, 4, 7, 2, 5

The inner loop is now terminated (j = 4 is not smaller than n-1).

We have one pass left in the outer loop (i=2, j=3). The comparison there is a[2] > a[3] which is 2 > 4 which is false and therefore the elements in b are not changed.

In the end your algorithm "sorts" the b array as 1, 4, 7, 2, 5.


How can you fix it

As stated in the first sentence of this answer: To sort an array you must compare the values to be sorted

In my example could this means to compare the values of the b array in the if statement.

In your code this means to compare the values of the BT array.


Just to be complete: your algorithm starts with i = 1 and therefore never sorts the first element (which, according to your comment, is desired).

It also never sorts the last element since the inner loop terminates before j reaches the last element. Is is unclear whether this is also desired.

If you want to sort including the last element then the inner loop must be

    for (int j = i + 1; j < n; j++)