As part of my QuickSort algorithm, I am trying to find the Median value from an Arraylist given the index of the first, middle and last elements. I used several if/else statements as a way of finding it, but it isn't right and the logic is rather convoluted.

Note: Sorting the array is not an option.

```
public static int findMedian(ArrayList <Integer> A, int first, int mid, int last) {
    if(first == mid || first == last || last == mid) {
        return first;
    }
    if(A.get(first) >= A.get(last)) {
        if(A.get(first) <= A.get(mid)) {
            return first;
        }
        else if(A.get(last) >= A.get(mid)) {
            return last;
        }
        return mid;
    }
    else {
        if(A.get(first) > A.get(mid)) {
            return first;
        }
        else if(A.get(mid) > A.get(last)) {
            return last;
        }
        return mid;
    }
}

```