【数据结构】归并排序

/**
	 * 合
	 * @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);
		}
	};

猜你喜欢

转载自blog.csdn.net/m0_37290323/article/details/80250792