So basically there are 3 condition on this code , we copare arr[mid] with 0 , 1 and 2.
I wrote the code like
class DutchNationalFlag {
public static void main(String []args) {
System.out.println("inside main");
int []arr = {2,1,1,0,0,2,1,2};
int []b = dutchSort(arr);
display(b);
}
public static int[] dutchSort(int []arr) {
System.out.println("inside dutchSort");
int len = arr.length;
int low =0 , mid = 0 , high = len-1;
System.out.println("value of low = " + low + " mid = " + mid + " high = " + high);
while(mid <= high) {
if(arr[mid] == 1){
mid ++;
System.out.println("value of low = " + low + " mid = " + mid + " high = " + high);
} else if(arr[mid] ==0) {
int temp = arr[low];
arr[low] = arr[mid];
arr[mid] = temp;
low++;
System.out.println("value of low = " + low + " mid = " + mid + " high = " + high);
} else if(arr[mid] ==2){
int temp1 = arr[high];
arr[high] = arr[mid];
arr[mid] = temp1;
high--;
System.out.println("value of low = " + low + " mid = " + mid + " high = " + high);
}
}
return arr;
}
public static void display(int []arr) {
for(int val : arr) {
System.out.print(val + " ");
}
System.out.println();
}
}
Though i have corrected my code and i did not use the second else if(arr[mid] == 2) and replaced it by just else
But i still haev no idea why did the code not work when i was using 2 else if