zoukankan      html  css  js  c++  java
  • java排序算法-归并排序

    public class MergeSort {
    
    	private static void mergeSortTest() {
    		int[] in = { 2, 5, 3, 8, 6, 7, 1, 4, 0, 9 };
    		Utils.printArray("归并排序前:",in);
    		int a[] = mergeSort(in);
    		Utils.printArray("归并排序后:",a);
    	}
    
    	private static int[] mergeSort(int[] arr) {
    		if (arr.length == 1) {
    			return arr;
    		} else {
    			int[] arrL = new int[arr.length / 2];
    			int[] arrR = new int[arr.length - arr.length / 2];
    			int mid = arr.length / 2;
    			for (int i = 0; i < mid; i++) {
    				arrL[i] = arr[i];
    			}
    			for (int i = mid, j = 0; i < arr.length; i++, j++) {
    				arrR[j] = arr[i];
    			}
    			int[] sortedArrL = mergeSort(arrL);
    			int[] sortedArrR = mergeSort(arrR);
    			int[] resultArr = mergeTwoArr(sortedArrL, sortedArrR);
    			return resultArr;
    		}
    	}
    
    	private static int[] mergeTwoArr(int[] arrL, int[] arrR) {
    		int i = 0, j = 0;
    		int[] arrTmp = new int[arrL.length + arrR.length];
    		int foot = 0;
    		while (i < arrL.length && j < arrR.length) {
    			if (arrL[i] <= arrR[j]) {
    				arrTmp[foot++] = arrL[i++];
    			} else {
    				arrTmp[foot++] = arrR[j++];
    			}
    		}
    		if (i == arrL.length) {
    			while (j < arrR.length) {
    				arrTmp[foot++] = arrR[j++];
    			}
    		} else {
    			while (i < arrL.length) {
    				arrTmp[foot++] = arrL[i++];
    			}
    		}
    		return arrTmp;
    	}
    	
    	public static void main(String[] args) {
    		mergeSortTest();
    	}
    }
    

      

  • 相关阅读:
    [学习笔记]康托展开
    [模板]平衡树
    [题解]涂色
    [学习笔记]Lucas定理
    欧拉定理及其证明
    一些杂题(排列组合
    swift 动画合集
    UIDynamicAnimator UIKit动力学
    swift 当协议遇见了泛型
    Swift 协议
  • 原文地址:https://www.cnblogs.com/zhaofeng555/p/3594875.html
Copyright © 2011-2022 走看看