七大基本排序算法之归并排序

import java.io.IOException;
import Input.InputString;

/**
 * 快速排序
 * @author xiaomi
 * 2012.04.02
 */
public class MergeSort {

	public static void main(String[] args) throws IOException{
		String s = InputString.getString();
		String[] str = s.split(" ");
		int[] a = new int[str.length];
		for(int i = 0;i < str.length;i++){
			a[i] = Integer.parseInt(str[i]);
		}
		mergeSort(a,0,a.length-1);
		for(int i = 0;i < a.length;i++){
			System.out.print(a[i]+" ");
		}
	}

	public static void mergeSort(int[] a,int low,int high){
		if(low == high){
			return;
		}
		int mid = (low+high)/2;
		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 i,j,k;
		i = low;
		j = mid+1;
		k = low;
		int[] temp = new int[a.length];
		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(i = low;i <= high;i++){
			a[i] = temp[i];
		}
	}
}

 

七大基本排序算法之冒泡排序

七大基本排序算法之选择排序

七大基本排序算法之插入排序

七大基本排序算法之希尔排序

七大基本排序算法之堆排序

七大基本排序算法之快速排序

七大基本排序算法之归并排序

你可能感兴趣的:(排序算法)