zoukankan      html  css  js  c++  java
  • 2路归并排序

    import java.util.Arrays;


    public class Merge_sort {
    public static void main(String[] args){
    int[] nums={ 2, 7, 8, 3, 1, 6, 9, 0, 5, 4 };
    sort(nums,0,nums.length-1);
    System.out.println(Arrays.toString(nums));
    }

    public static int[] sort(int[] num,int low,int high){
    int mid=(low+high)/2;
    if(low<high){ 
    //左排序
    sort(num,low,mid);
    //右排序
    sort(num,mid+1,high);
    Merge(num, low,mid, high);
    }
    return num;
    }

    public static void Merge(int[] num,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(num[i]<=num[j]){
    temp[k]=num[i];
    i++;
    }else{
    temp[k]=num[j];
    j++;
    }
    k++;
    }
    while(i<=mid){
    temp[k]=num[i];
    k++;
    i++;
    }
    while(j<=high){
    temp[k]=num[j];
    k++;
    j++;
    }

    for(int m=0;m<temp.length;m++){
    num[low++]=temp[m];

    }
    }

    }

  • 相关阅读:
    魔兽登录系统
    航班信息查询预订
    第六章
    嵌套.
    嵌套
    Mysql
    第二章
    Java
    HTML-表格-列表-结构标记-表单
    HTML语言
  • 原文地址:https://www.cnblogs.com/catWang/p/4372861.html
Copyright © 2011-2022 走看看