算法:归并排序,数组和链表

数组:

import java.util.Arrays;
public class MergeSort {
     public static void main(String[] args) {
          int[] arr=new int[]{8,3,5,9,1,7,3,10,22,41};
          mergeSort(arr);
          System.out.println(Arrays.toString(arr));
     }
     
     private static void mergeSort(int[] arr){
          if(arr.length>1){
              int[] half1 = new int[arr.length/2];
              int[] half2 = new int[arr.length - arr.length/2];
              System.arraycopy(arr, 0, half1, 0, arr.length/2);
              System.arraycopy(arr, arr.length/2, half2, 0, arr.length - arr.length/2);
              mergeSort(half1);
              mergeSort(half2);
              merge(half1,half2,arr);
          }
     }
     private static void merge(int[] half1,int[] half2,int[] arr){
          int current1 = 0,current2 = 0,current3 = 0;
          while(current1 < half1.length && current2 < half2.length){
              if(half1[current1] < half2[current2]){
                   arr[current3++] = half1[current1++];
              }else{
                   arr[current3++] = half2[current2++];
              }
          }
          while(current1 < half1.length)
              arr[current3++]=half1[current1++];
          while(current2 < half2.length)
              arr[current3++]=half2[current2++];
     }
}

链表:

import java.util.Collections;
import java.util.LinkedList;
public class LinkedListMerge {
    public static void main(String[] args) {
        LinkedList<Integer> list = new LinkedList<Integer>();
        for(int i = 0;i<1000;i++){
            list.add(i);
        }
        Collections.shuffle(list);
        System.out.println(list);
        mergeSort(list);
        System.out.println(list);
    }
    public static void mergeSort(LinkedList<Integer> list){
        if(list.size()>1){
            LinkedList<Integer> half1 = new LinkedList<Integer>();
            LinkedList<Integer> half2 = new LinkedList<Integer>();
            while(list.size()>0){
                half1.add(list.poll());
                if(list.size()>0){
                    half2.add(list.removeLast());
                }
            }
            mergeSort(half1);
            mergeSort(half2);
            merge(half1,half2,list);
        }
    }
    //这个合并方法是比较优雅的,非常容易理解
    public static void merge(LinkedList<Integer> list1,LinkedList<Integer> list2,LinkedList<Integer> result){
        while(list1.peek()!=null && list2.peek()!=null){
            if(list1.peek() > list2.peek()){
                result.add(list2.poll());
            }else{
                result.add(list1.poll());
            }
        }
        while(list1.peek()!=null){
            result.add(list1.poll());
        }
        while(list2.peek()!=null){
            result.add(list2.poll());
        }
    }
}

几乎一样,但是链表的更加优雅一些。

转载于:https://my.oschina.net/wuxiaofei/blog/1612734

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值