/**
* 合
* @param a
* @param low
* @param mid
* @param high
*/
public static void mergeSortMerge(int[] a, int low, int mid, int high) {
int[] temp = new int[high - low + 1];
int indexLeft = low;
int indexRight = mid + 1;
int k = 0;
while (indexLeft <=mid && indexRight <=high) {
if (a[indexLeft] < a[indexRight]) {
temp[k] = a[indexLeft];
indexLeft++;
k++;
} else {
temp[k] = a[indexRight];
indexRight++;
k++;
}
}
while(indexLeft<=mid) {
temp[k]=a[indexLeft];
indexLeft++;
k++;
}
while(indexRight<=high) {
temp[k]=a[indexRight];
indexRight++;
k++;
}
for(int j=0;j<temp.length;j++)
{
a[low+j]=temp[j];
}
};
/**
* 分
* @param a
* @param low
* @param high
*/
public static void mergeSortSplit(int[] a, int low, int high) {
int mid = (low+high)/2;
if(low<high)
{
//System.out.println(low+" "+high);
mergeSortSplit(a, low, mid);
mergeSortSplit(a, mid + 1, high);
mergeSortMerge(a,low,mid,high);
}
};
【数据结构】归并排序//两个有序数组的合并
最新推荐文章于 2022-08-16 21:42:53 发布