| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 
 | import java.util.Arrays;import java.util.Random;
 
 public class MergeSort {
 public static void main(String[] args) {
 int[] sample = new Random().ints(0, 100).limit(10).toArray();
 System.out.println("Origin: " + Arrays.toString(sample));
 
 mergeSort(sample, 0, sample.length - 1);
 System.out.println("Origin: " + Arrays.toString(sample));
 }
 
 private static void mergeSort(int[] sample, int low, int high) {
 
 if (low >= high)
 return;
 
 int mid = (low + high) / 2;
 
 mergeSort(sample, low, mid);
 mergeSort(sample, mid + 1, high);
 
 merge(sample, low, mid, high);
 }
 
 private static void merge(int[] sample, int low, int mid, int high) {
 
 int[] tmp = new int[high - low + 1];
 int i = low, j = mid + 1;
 int index = 0;
 
 while (i <= mid && j <= high) {
 if (sample[i] < sample[j]) {
 tmp[index++] = sample[i++];
 } else {
 tmp[index++] = sample[j++];
 }
 }
 
 
 while (i <= mid) {
 tmp[index++] = sample[i++];
 }
 
 while (j <= high) {
 tmp[index++] = sample[j++];
 }
 
 
 for (int pos = 0; pos < tmp.length; pos++) {
 sample[low + pos] = tmp[pos];
 }
 }
 }
 
 |