Skip to content

Instantly share code, notes, and snippets.

@Argygle
Created March 24, 2016 05:02
Show Gist options
  • Save Argygle/478799d326a8accc4329 to your computer and use it in GitHub Desktop.
Save Argygle/478799d326a8accc4329 to your computer and use it in GitHub Desktop.
public class MultiMergeSort {
public static void mergeSort(int[] arr, int low, int high) {
int size = arr.length;
if (low<high) {
int middle = (low + high) / 2;
mergeSort(arr, low, middle);
mergeSort(arr, middle+1, high);
merge(arr, low, middle);
}
}
public static void merge (int[] arr, int low, int high) {
int size = arr.length;
int[] temp = new int[size];
for(int i = low; i<= high; i++) {
temp[i] = arr[i];
}
int i = low;
int j = middle + 1;
int k = low;
while (i<=middle && j <= high) {
if (temp[i] <= temp[j]) {
arr[k] = temp[i];
i++;
}
else {
arr[k] = temp[j];
j++;
}
k++;
}
while (i<= middle) {
arr[k] = temp[i];
k++;
i++;
}
}
public static void main (String[] args) {
int size = 10;
int[] data = new int[]{5,10,1,6,2,9,3,8,7,4};
int low = 0;
int high = 9;
System.out.println("Before Sort: ");
for(int i=0; i<size; i++) {
System.out.println(data[i]);;
}
mergeSort(data,low,high);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment