Created
March 24, 2016 05:53
-
-
Save Argygle/77ea7261849c9082d6cb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MultiMergeSort { | |
private static int middle; | |
public static void mergeSort(int[] arr, int low, int high) { | |
int size = arr.length; | |
if (low<high) { | |
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); | |
System.out.println("After Sort: "+data); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment