算法笔记 - 数组与单链表快速排序(Java)

3 篇文章 0 订阅

数组快速排序

public class QuickSort {
    private static final int COUNT = 1000000;
    private static final int ZONE = 200;

    public static void main(String[] args){
        Random rand = new Random();
        Integer[] array = new Integer[COUNT];
        for(int i=0;i<COUNT;i++){
            array[i] = rand.nextInt(ZONE);
        }

        long startTime = System.currentTimeMillis();
        quickSort(array,0,array.length-1);
        long endTime = System.currentTimeMillis();
        float time = (endTime - startTime) / 1000F;

        System.out.println(time);
    }

    private static void quickSort(Integer[] array,int left,int right){
        if(left>=right) return;
        int i = left;
        int j = right;
        int key = array[(i+j)/2];
        while(i<j){
            for(;i<j&&array[j]>=key;j--);
            array[i] = array[j];

            for(;i<j&&array[i]<=key;i++);
            array[j] = array[i];
        }
        array[i] = key;
        quickSort(array,left,i-1);
        quickSort(array,i+1,right);
    }
}

单链表快速排序

public class LinkedQuickSort {

    private static final int COUNT = 100000;
    private static final int ZONE = 200;

    public static void main(String[] args){
        Random rand = new Random();
        Node node = new Node(0);
        Node head = node;

        for(int i=0;i<COUNT;i++){
            node.next = new Node(rand.nextInt(ZONE));
            node = node.next;
        }

        long startTime = System.currentTimeMillis();
        quickSort(head.next);
        long endTime = System.currentTimeMillis();
        float time = (endTime - startTime) / 1000F;

        System.out.println(time);

        for(head=head.next;head!=null;head=head.next){
            System.out.print(head.val+" ");
        }
    }

    private static Node quickSort(Node head){
        if(head == null || head.next == null) return head;

        Node left = new Node(0);
        Node middle = new Node(0);
        Node right = new Node(0);
        Node leftHead = left;
        Node middleHead = middle;
        Node rightHead = right;
        int val = head.val;

        for(;head!=null;head=head.next){
            if(head.val<val){
                left.next = head;
                left = left.next;
            }else if(head.val>val){
                right.next = head;
                right = right.next;
            }else{
                middle.next = head;
                middle = middle.next;
            }
        }

        left.next = null;
        middle.next = null;
        right.next = null;
        return merge(quickSort(leftHead.next),middleHead.next,quickSort(rightHead.next));
    }

    private static Node merge(Node left, Node middle, Node right) {
        Node leftTail = findTail(left);
        Node middleTail = findTail(middle);
        middleTail.next = right;
        if(left!=null){
            leftTail.next = middle;
            return left;
        }else{
            return middle;
        }
    }

    private static Node findTail(Node node){
        Node tail = node;
        if(tail != null){
            while(tail.next!=null){
                tail = tail.next;
            }
        }
        return tail;
    }
}

class Node{
    int val;
    Node next;
    public Node(int val) {
        this.val = val;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值