Bottom-up Java Implementation

suggest change
import java.util.Arrays;

public class MergeSortBU {
    public static <T extends Comparable<? super T>> void sort(T[] values) {
        T[] auxiliary = values.clone();
        for (int width = 1; width < values.length; width *= 2) {
            for (int first = 0; first < values.length - width; first += 2 * width) {
                int middle = first + width;
                int last = Math.min(first + 2 * width, values.length);
                merge(values, auxiliary, first, middle, last);
            }
        }
    }

    private static <T extends Comparable<? super T>> void merge(
            T[] values, T[] auxiliary, int first, int middle, int last) {
        System.arraycopy(values, first, auxiliary, first, last - first);
        int left = first;
        int right = middle;
        for (int destination = first; destination < last; destination++) {
            if (left == middle) values[destination] = auxiliary[right++];
            else if (right == last) values[destination] = auxiliary[left++];
            else if (auxiliary[left].compareTo(auxiliary[right]) <= 0)
                values[destination] = auxiliary[left++];
            else values[destination] = auxiliary[right++];
        }
    }

    public static void main(String[] args) {
        Integer[] values = {4, 3, 1, 8, 9, 15, 20, 2, 5, 6};
        sort(values);
        System.out.println(Arrays.toString(values));
    }
}
[1, 2, 3, 4, 5, 6, 8, 9, 15, 20]

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents