Trying Improve Maxmin Method Number Comparisons 3n 2 2 Method Attach Give Number Comparis Q10372627

I am trying to improve the MaxMin method such that the number ofcomparisons is ⌈3n/2−2⌉. the method I attach below only give me thenumber of comparison is 2+C(⌊n/2⌋)+C(⌈n/2⌉). please help, thankyou

public class MinMax {
  
  
public static void main(String[] args) {
int[] array = new int[]{4, 7, 2, 10, 345, 28, 1, 5, 0, -10, -4098,23, 0};

Pair expected = new Pair(-4098, 345);
Pair actual = MinMax.mmB(0, array.length-1, array);
if (actual.alpha != expected.alpha) {
System.out.println(“Min value incorrect. Expected:”+expected.alpha+” but got: “+actual.alpha);
} else if (actual.omega != expected.omega) {
System.out.println(“Max value incorrect. Expected:”+expected.omega+” but got: “+actual.omega);
} else {
System.out.println(“Test Passed”+actual.alpha +” “+actual.omega);
}
}

public static Pair mmA(int lB, int uB, int[] array){
if (uB == lB){
return new Pair(array[lB], array[uB]);
}
else if (uB – lB == 1) {
int smallest = Math.min(array[lB], array[uB]);
int largest = Math.max(array[lB], array[uB]);
return new Pair(smallest, largest);
}
else {
int mid = (lB + uB)/2;
Pair First = mmA(lB, mid, array);
Pair Next = mmA(mid+1, uB, array);
int smallest = Math.min(First.alpha, Next.alpha);
int largest= Math.max(First.omega, Next.omega);
return new Pair(smallest, largest);
}
}

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *