快速排序,归并排序

1.快速排序

如何记忆快速排序

1.首先明确函数,快速排序是基于一个pivot的,所以应该需要对数组的一部分进行排序,所以应该传入left和right范围
2.需要一个函数来确定partition,确定一个pivot为nums[left],选择第一个元素,然后确定这个元素在子数组中的位置,是基于交换进行的,所以还需要swap函数。找到第一个比pivot大的和比pivot小的数,然后进行交换。
3.最后循环跳出的时候,把pivot放到右指针的位置
代码如下
注意!!
第二个quickSort函数需要判断边界条件,否则会出现死循环
除了第一个quickSort,别的函数都不需要返回值

public int[] quickSort(int[] nums){
        quickSort(nums, 0, nums.length - 1);
        return nums;
    }

    public void quickSort(int[] nums,int left,int right){
        if (left>=right) return;
        int partition = partition(nums, left, right);
        quickSort(nums,left,partition-1);
        quickSort(nums,partition+1,right);
    }

    public int partition(int[] nums, int left,int right){
        int pivot = nums[left];
        int lo = left;
        int hi = right+1;
        while (true){
            while (nums[++lo]<pivot){
                if (lo==right) break;
            }

            while (nums[--hi]>pivot){
                if (hi==left) break;
            }
            if (lo>=hi) break;
            swap(nums, lo, hi);
        }
        swap(nums,left,hi);
        return hi;
    }

    public void swap(int[] nums, int lo, int hi){
        int tmp = nums[lo];
        nums[lo] = nums[hi];
        nums[hi] = tmp;
    }

    public static void main(String[] args) {
        QuickSort x = new QuickSort();
        int[] nums = {3,4,1,2,6,7,4,9,11};
        int[] ints = x.quickSort(nums);
        for (int anInt : ints) {
            System.out.println(anInt);
        }
    }

2.归并排序

归并排序的代码如下,每天都复习

public class MergeSort {
    int[] temp;
    public void sort(int[] nums){
        temp = new int[nums.length];
        sort(nums,0,nums.length-1);
    }

    public void sort(int[] nums,int lo, int hi){
        if (lo == hi ) return;
        int mid = lo + (hi-lo) / 2;
        sort(nums,lo,mid);
        sort(nums,mid+1,hi);
        merge(nums,lo,mid,hi);
    }

    public void merge(int[] nums,int lo,int mid,int hi){
        for (int i = lo; i <= hi; i++) {
            temp[i] = nums[i];
        }

        int i = lo,j = mid+1;
        int p = lo;
        
        while (i!=mid+1&&j!=hi+1){
            if (temp[i]<temp[j]){
                nums[p++] = temp[i++];
            }
            else {
                nums[p++] = temp[j++];
            }
        }
        while (i!=mid+1) nums[p++] = temp[i++];
        while (j!=hi+1) nums[p++] = temp[j++];
    }

    public static void main(String[] args) {
        MergeSort x = new MergeSort();
        int[] nums = {1,3,4,1,2,3,9,6,5,4,11};
        x.sort(nums);
        for (int num : nums) {
            System.out.println(num);
        }
    }

3.排序链表

在这里插入图片描述
思路:使用归并排序的思想,将原来的链表裂开成为两个链表,然后进行merge
找到链表的中点,使用快慢指针
如果链表是奇数,则slow指向中点,如果链表是偶数,则slow.next是中点

ListNode slow = head, fast = head.next;
        while (fast!=null && fast.next!=null){
            slow = slow.next;
            fast = fast.next.next;
        }

代码如下

public ListNode sortList(ListNode head) {
        if (head == null || head.next==null){
            return head;
        }
        ListNode slow = head, fast = head.next;
        while (fast!=null && fast.next!=null){
            slow = slow.next;
            fast = fast.next.next;
        }
        ListNode tmp = slow.next;
        slow.next = null;
        ListNode left = sortList(head);
        ListNode right = sortList(tmp);
        ListNode h = new ListNode(-1);
        ListNode res = h;
        while (left!=null && right!=null){
            if (left.val<right.val) {
                h.next = left;
                left = left.next;
            }else {
                h.next = right;
                right = right.next;
            }
            h = h.next;
        }
        if (left!=null) {
            h.next = left;
        }
        if (right!=null) {
            h.next = right;
        }
        return res.next;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值