面试之排序算法

目录

快速排序

归并排序


一、快速排序

1、针对数组

https://leetcode.cn/problems/sort-an-array/description/

class Solution {
    int l;
    public int[] sortArray(int[] nums) {
        l = nums.length;

        sort(nums, 0, l-1);
        return nums;
    }

    public void sort(int[] nums, int left, int right) {
        if(left>=right) return;
        //随机基准,不加这里leetcode超时
        //int random = (int)(left+Math.random()*(right-left+1));
        //swap(nums, random, right);
        int pos = quik(nums, left, right);
        sort(nums, left, pos-1);
        sort(nums, pos+1, right);
    }

    public int quik(int[] nums, int left, int right) {
        int begin = left;
        int end = right;
        int key = right;

        while(begin<end){
            while(begin<end && nums[begin] <= nums[key]) begin++;
            while(begin<end && nums[end] >= nums[key]) end--;
            swap(nums, begin, end);
        }
        swap(nums, begin, right);//交换基准
        return begin;//返回基准
    }

    public void swap(int nums[], int a, int b) {
        int tmp = nums[a];
        nums[a] = nums[b];
        nums[b] = tmp;
    }
}

2、针对链表

二、归并排序

1、针对数组

力扣

class Solution {
    public int[] sortArray(int[] nums) {
        return sort(nums);
    }

    public int[] sort(int nums[]) {
        //终止条件处理
        if (nums.length<2) return nums;
        int mid=nums.length/2;
        int left[] = Arrays.copyOfRange(nums,0,mid);
        int right[] = Arrays.copyOfRange(nums,mid,nums.length);
        
        return merge(sort(left),sort(right));
    }

    public int[] merge(int a[], int b[]) {
        int res[] = new int[a.length + b.length];
        int pos=0,i = 0, j = 0;

        while(i<a.length && j < b.length) {
            if (a[i] < b[j]) {
                res[pos++] = a[i++];
            } else {
                res[pos++] = b[j++];
            }
        }
        while(i<a.length) {
            res[pos++] = a[i++];
        }
        while(j<b.length) {
            res[pos++] = b[j++];
        }
        return res;
    }
}

2、针对链表

自顶向下的归并排序(面试不能用)

/**
 * Definition for singly-linked list.
 * public 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 sort(head, null);
    }
    public ListNode sort(ListNode head,ListNode tail) {
        //
        if (head==null) return head;
        if(head.next==tail){
            head.next=null;// 一个节点的处理
            return head;
        }
        //find mid
        ListNode fast=head, slow=head;
        while(fast!=tail){
            slow=slow.next;
            fast=fast.next;
            if(fast!=tail) fast=fast.next;
        }
        ListNode mid = slow;

        ListNode a = sort(head, mid);
        ListNode b = sort(mid,tail);
        return merge(a,b);
    }

    public ListNode merge(ListNode a, ListNode b) {
        ListNode res = new ListNode(0);
        ListNode tmp = res;

        while(a!=null && b!=null){
            if(a.val<b.val){
                tmp.next=a;
                a=a.next;
            }else {
                tmp.next=b;
                b=b.next;
            }
            tmp=tmp.next;
        }
        if(a!=null) tmp.next=a;
        if(b!=null) tmp.next=b;

        return res.next;
    }



}

时间复杂度:nlogn

控件复杂度 logn

自底向上(面试用)

优化空间复杂度为1

/**
 * Definition for singly-linked list.
 * public 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) {
        if(head==null) return head;
        //计数
        int n = 0;
        ListNode node = head;
        while(node!=null){
            n++;
            node=node.next;
        }
        //初始化
        ListNode tmp = new ListNode(0);
        tmp.next=head;

        //计算
        for(int subLen = 1; subLen < n; subLen*=2) {
            ListNode pre=tmp;
            ListNode cur=tmp.next;

            while(cur!=null){
                //链表1
                ListNode a=cur;//头
                for(int i=1;i<subLen&&cur!=null&&cur.next!=null; i++){
                    cur=cur.next;
                }
                //链表2
                ListNode b=cur.next;//头
                cur.next=null;//cut
                cur=b;
                for(int i=1;i<subLen&&cur!=null&&cur.next!=null; i++){
                    cur=cur.next;
                }
                //断后
                ListNode next=null;
                if(cur!=null){
                    next=cur.next;//记录
                    cur.next=null;//cut
                }
                //merge
                ListNode mer = merge(a,b);
                pre.next=mer;
                while(pre.next!=null){ //move
                    pre=pre.next;
                }
                cur=next;

            }


        }

        return tmp.next;
    }

    public ListNode merge(ListNode a, ListNode b) {
        ListNode res = new ListNode(0);
        ListNode tmp = res;

        while(a!=null && b!=null){
            if(a.val<b.val){
                tmp.next=a;
                a=a.next;
            }else {
                tmp.next=b;
                b=b.next;
            }
            tmp=tmp.next;
        }
        if(a!=null) tmp.next=a;
        if(b!=null) tmp.next=b;

        return res.next;
    }









}

区间排序

力扣

class Solution {
    public int[][] merge(int[][] intervals) {
        int index = -1;
        int res[][]=new int[intervals.length][2];
        Arrays.sort(intervals, (a,b) -> a[0]-b[0]);

        for(int[] a : intervals) {
            if(index==-1 || a[0]>res[index][1]) res[++index]=a;
            else {
                res[index][1]=Math.max(res[index][1], a[1]);
            }
        }
        return Arrays.copyOf(res,index+1);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值