Skip to content

Instantly share code, notes, and snippets.

@memish
Last active January 14, 2019 14:09
Show Gist options
  • Save memish/b866cbe6a2d165793b7e5fc671648c6c to your computer and use it in GitHub Desktop.
Save memish/b866cbe6a2d165793b7e5fc671648c6c to your computer and use it in GitHub Desktop.
public class MergeSortClass
{
// instance variables - replace the example below with your own
public MergeSortClass()
{
int[] actual = { 5, 8, 1, 6, 7, 2, 3, 4 };
// end result { 1, 2, 3, 4, 5, 6,7,8 };
// for (int i = 0; i < actual.length; i++)
// System.out.print(actual[i]);
mergeSort(actual, actual.length);
System.out.println();
// for (int i = 0; i < actual.length; i++)
// System.out.print(actual[i]);
}
public static void mergeSort(int[] a, int n) {
if (n < 2) {//array of size one is sorted :)
return;
}
int mid = n / 2;//get the midpoint
int[] l = new int[mid];
int[] r = new int[n - mid];
//CODE HERE
mergeSort(l, mid);
mergeSort(r, n - mid);
merge(a, l, r, mid, n - mid);
}
public static void merge(
int[] a, int[] l, int[] r, int left, int right) {
//below is just for testing purposes
for (int x = 0; x < a.length;x++)
System.out.print(a[x]);
System.out.println("");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment