zoukankan      html  css  js  c++  java
  • 合并排序法-Java实现

    public static void mergeSort(int[] a, int low, int high) { //调用mergeSort方法时low为0, high为a.length-1
    		int mid = (low + high) / 2;
    		if (low < high) {
    			MergeSort(a, low, mid);
    			MergeSort(a, mid + 1, high);
    			merge(a, low, mid, high);
    		}
    	}
    
    	public static void merge(int[] a, int low, int mid, int high) {
    		int[] temp = new int[high - low + 1];
    		int i = low;
    		int j = mid + 1;
    		int k = 0;
    		while (i <= mid && j <= high) {
    			if (a[i] < a[j])
    				temp[k++] = a[i++];
    			else
    				temp[k++] = a[j++];
    		}
    		while (i <= mid)
    			temp[k++] = a[i++];
    		while (j <= high)
    			temp[k++] = a[j++];
    		for (int index = 0; index < temp.length; index++)
    			a[index + low] = temp[index];
    	}
    苟利国家生死以, 岂因祸福避趋之
  • 相关阅读:
    NIO学习
    XML(二)
    IO和NIO
    Log4j
    异常处理机制
    XML
    数据交互
    分页实现的三种方式
    Idea破解
    数据库连接池
  • 原文地址:https://www.cnblogs.com/chintsai/p/10117044.html
Copyright © 2011-2022 走看看