You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
62 lines
1.3 KiB
62 lines
1.3 KiB
/*
|
|
* To change this license header, choose License Headers in Project Properties.
|
|
* To change this template file, choose Tools | Templates
|
|
* and open the template in the editor.
|
|
*/
|
|
|
|
/**
|
|
*
|
|
* @author lenovo
|
|
*/
|
|
public class MergeSort {
|
|
|
|
private static int[] aux;
|
|
|
|
public static void merge(int[] a, int low, int mid, int high)
|
|
{
|
|
int i = low;
|
|
int j = mid + 1;
|
|
|
|
for(int k=low; k<=high; k++)
|
|
{
|
|
aux[k] = a[k];
|
|
}
|
|
|
|
for(int k=low; k<=high; k++)
|
|
{
|
|
if(i>mid)
|
|
{
|
|
a[k] = aux[j++];
|
|
}
|
|
else if(j > high)
|
|
{
|
|
a[k] = aux[i++];
|
|
}
|
|
else if (aux[j] < aux[i])
|
|
{
|
|
a[k] = aux[j++];
|
|
}
|
|
else
|
|
{
|
|
a[k] = aux[i++];
|
|
}
|
|
}
|
|
}
|
|
|
|
private static void sort(int[] a, int low, int high)
|
|
{
|
|
if(high <= low) return;
|
|
|
|
int mid = low + (high - low) / 2;
|
|
sort(a, low, mid);
|
|
sort(a, mid+1, high);
|
|
merge(a, low, mid, high);
|
|
}
|
|
public void sort(int[] a)
|
|
{
|
|
int length = a.length;
|
|
aux = new int[length];
|
|
sort(a, 0, length-1);
|
|
}
|
|
}
|