[Data Structure] Merge Sort

/**
	 * combine
	 * @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];
		}

	};

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325968760&siteId=291194637