Finding median of 3 elements in array, and sorting them
You seem to specify that it will only be 3 elements in the title of the question, but your input shows you input more than 3 every so often. Your sorting "algorithm" (a lot of swaps) is quite naïve in that sense - for examples of some sorting algorithms that you could implement, take a look at Wikipedia's article on Sorting Algorithms. No need to understand all of them - some get quite complex!

However, you don't have to write a sorting algorithm yourself to use one! Java has some built in for you.

import java.util.Arrays; // or java.util.Collections if you are using ArrayList etc
Arrays.sort(my_array); // this will sort your array for you, using the Quicksort algorithm


As @JS1 mentioned, your formula for finding the center is wrong, and should be

int center = start + (end - start) / 2;


And with that, you can simply return the center index of the array, since it's all sorted now!

public static int medianOfThree(int[] sample, int start, int end) {
    Arrays.sort(sample);
    return sample[start + (end - start) / 2];
}


...Unless the array has a length that is an even number. Then the median formula is a little different:

public static int medianOfFour(int[] sample, int start, int end) {
    Arrays.sort(sample);
    int medianRightIndex = start + (end - start) / 2;
    int medianLeftIndex = medianRightIndex - 1;
    return (sample[medianRightIndex] + sample[medianLeftIndex]) / 2;
}


However, unless you are specifying a range in the array (e.g. you only wanted the median of the first 3 elements), you don't need to specify the start and end parameters at all. start can be 0 and end can be sample.length. Again, this is only if you want the median of the entire array. For a specific subset, you'd still use start and end parameters.

Now we have arrived to:

public static int median(int[] sample) {
    Arrays.sort(sample);
    int median = 0;
    int index = sample.length / 2;
    if(sample.length % 2 == 0) {
        median = (sample[index] - sample[index - 1]) / 2;
    } else {
        median = sample[index];
    }
    return median;
}