Skip to content

Instantly share code, notes, and snippets.

@metallurgix
Last active August 29, 2015 14:03
Show Gist options
  • Save metallurgix/133720274b142553a9bd to your computer and use it in GitHub Desktop.
Save metallurgix/133720274b142553a9bd to your computer and use it in GitHub Desktop.
Mege Sort
public class MergeSorter
{
private static int[] a, b;
public static void sort(int[] c)
{
a=c;
int n=a.length;
b=new int[(n+1)/2];
mergesort(0, n-1);
}
private static void mergesort(int lo, int hi)
{
if (lo<hi)
{
int m=(lo+hi)/2;
mergesort(lo, m);
mergesort(m+1, hi);
merge(lo, m, hi);
}
}
private static void merge(int lo, int m, int hi)
{
int i, j, k;
i=0; j=lo;
while (j<=m)
b[i++]=a[j++];
i=0; k=lo;
while (k<j && j<=hi)
if (b[i]<=a[j])
a[k++]=b[i++];
else
a[k++]=a[j++];
while (k<j)
a[k++]=b[i++];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment