快排和归并排序

链表定义


class ListNode {
    int val;
    ListNode next;

    ListNode() {
    }

    ListNode(int val) {
        this.val = val;
    }

    ListNode(int val, ListNode next) {
        this.val = val;
        this.next = next;
    }
}

链表-快排


class Solution {
    public ListNode sortList(ListNode head) {
        return quickSort(head);
    }

    public ListNode quickSort(ListNode head) {
        if (head == null || head.next == null) return head;
        ListNode smallDummy = new ListNode(0);
        ListNode bigDummy = new ListNode(0);
        ListNode s = smallDummy, b = bigDummy, node = head.next;
        while (node != null) {
            if (node.val < head.val) {
                s.next = node;
                s = s.next;
            } else {
                b.next = node;
                b = b.next;
            }
            node = node.next;
        }
        //注意b后面可能有多余的next
        b.next = null;
        s.next = head;
        //斩断s和b
        head.next = null;
        s = quickSort(smallDummy.next);
        b = quickSort(bigDummy.next);
        head.next = b;
        //此处不能返回smallDummy.next,可能他的指针已经被修改了。
        return s;
    }
}

#链表-归并排序

数组-快排

class Solution {
  
    public int[] sortArray(int[] nums) {
        int len=nums.length;
        quickSort(nums,0,len-1);
        return nums;
    }
   public void quickSort(int [] nums, int left, int right){
        if(left<right){
            int mid=getMid(nums, left, right);
            quickSort(nums,left,mid-1);
            quickSort(nums,mid+1,right);
        }
    }
    public int getMid(int[]nums,int left,int right){
        int L=left,R=right;
        int keyIndex=new Random().nextInt(R-L+1)+L;
        swap(nums,L,keyIndex);
        while(L < R){
            while(L < R && nums[R]>=nums[left])R--;
            while(L < R && nums[L]<=nums[left])L++;
            swap(nums,L,R);
        }
        swap(nums,L,left);
        return L;
    }
    public void swap(int[] nums, int i, int j){
        int temp = nums[i];
        nums[i] = nums[j];
        nums[j] = temp;
    }

}

链表-归并排序


class Solution {
    public ListNode sortList(ListNode head) {
        return mergeSort(head, null);
    }
    public ListNode mergeSort(ListNode head, ListNode tail) {
        if (head != tail) {
            ListNode fast = head;
            ListNode slow = head;
            while (fast != tail && fast.next != tail) {
                fast = fast.next.next;
                slow = slow.next;
            }
            ListNode mid = slow.next;
            slow.next = null;
            ListNode left = mergeSort(head, slow);
            ListNode right = mergeSort(mid, tail);
            return merge(left, right);
        } else {
            return head;
        }
    }

    public ListNode merge(ListNode left, ListNode right) {
        ListNode dummy = new ListNode(0);
        ListNode cur = dummy;
        while (right != null && left != null) {
            if (left.val > right.val) {
                cur.next = right;
                right = right.next;

            } else {
                cur.next = left;
                left = left.next;
            }
            cur = cur.next;
        }
        if (left != null) {
            cur.next = left;
        }
        if (right != null) {
            cur.next = right;
        }
        return dummy.next;
    }
}

数组-归并排序

class Solution {
  
    public int[] sortArray(int[] nums) {
        int len=nums.length;
        mergeSort(nums,0,len-1);
        return nums;
    }
    public void mergeSort(int[] nums, int left, int right){
        if(left < right){
            int mid = left + (right - left) / 2;
            mergeSort(nums, left, mid);
            mergeSort(nums, mid+1, right);
            merge(nums, left, right, mid);
        }
    }
    public void merge(int[] nums, int left, int right, int mid){
        int []temp=new int[right - left + 1];
        int leftPoint=left, rightPoint=mid+1,point=0;
        while(leftPoint <= mid && rightPoint <= right){
            if(nums[leftPoint] <= nums[rightPoint]){
                temp[point++] = nums[leftPoint++];
            }else{
                // res += (mid-leftPoint+1);
                temp[point++] = nums[rightPoint++];
            }
        }
        while(leftPoint <=mid){
            temp[point++] = nums[leftPoint++];
        }
        while(rightPoint <=right){
            temp[point++] = nums[rightPoint++];
        }
        for(int i=0;i < temp.length; i++){
            nums[i+left] = temp[i];
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值