算法--2023.1.12

1.力扣148--排序链表
class Solution {
    //迭代法的递归排序,时间复杂度为o(1)
    public ListNode sortList(ListNode head) {
        int n = 0;
        ListNode l = head, r = head;
        //遍历整个人链表,计算节点的个数n
        while(r!=null){
            n++;
            r = r.next;
        }
        //迭代的从长度为1开始归并
        for(int i = 1;i<n;i = i*2){
            //在每层归并后,我们需要记录新的一层的头节点,dummy.next
            ListNode dummy = new ListNode(-1);
            //在每层归并时,我们需要记录当前节点的指针cur
            ListNode cur = dummy;
            for(int j = 1;j<=n;j = j + 2*i){
                //在每层遍历中,每次向前走i*2步长,在i*2内个节点排序
                ListNode p = head, q = p;
                for(int k = 0;k<i&&q!=null;k++){
                    q = q.next;
                }
                //我们用o来记录下一段的头节点
                ListNode o = q;
                for(int k = 0;k<i&&o!=null;k++){
                    o = o.next;
                }
                //两小段归并排序的过程
                int left = 0,right = 0;
                while(left<i&&right<i&&p!=null&&q!=null){
                    if(p.val<q.val){
                        cur.next = p;
                        p = p.next;
                        cur = cur.next;
                        left++;
                    }else{
                        cur.next = q;
                        q = q.next;
                        cur = cur.next;
                        right++;
                    }
                }
                while(left<i&&p!=null){
                    cur.next = p;
                    p = p.next;
                    cur = cur.next;
                    left++;
                }
                while(right<i&&q!=null){
                    cur.next = q;
                    q = q.next;
                    cur = cur.next;
                    right++;
                }
                head = o;
            }
            cur.next = null;
            head = dummy.next;
        }
        return head;
    }
}

  

2.acwing787--归并排序

import java.util.Scanner;

public class acwing787 {
    public  static int N = 1000010;
    public  static int[] nums = new int[N];
    public static void mergeSort(int l, int r){
        if(l>=r){
            return;
        }
        int mid = (l+r)/2;
        mergeSort(l,mid);
        mergeSort(mid+1,r);
        int left = l, right = mid+1, k = 0;
        int[] temp = new int[r-l+1];
        while(left<=mid&&right<=r){
            if(nums[left]<nums[right]){
                temp[k++] = nums[left++];
            }else{
                temp[k++] = nums[right++];
            }
        }
        while(left<=mid){
            temp[k++] = nums[left++];
        }
        while(right<=r){
            temp[k++] = nums[right++];
        }
        for(int i = 0;i<k;i++){
            nums[l+i] = temp[i];
        }
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        nums = new int[n];
        for(int i = 0;i<n;i++){
            nums[i] = in.nextInt();
        }
        mergeSort(0,nums.length-1);
        for (int i = 0;i<n;i++){
            System.out.print(nums[i] + " ");
        }
    }
}

 3.力扣51--数组中的逆序对

 

class Solution {
    //可以用递归的对逆排序来计算
    public int[] nums;
    public int reversePairs(int[] nums) {
        this.nums = nums;
        return mergeSort(0,nums.length-1);
    }
    public int mergeSort(int left ,int right){
        if(left>=right){
            return 0;
        }
        int mid = (left+right)/2; 
        int res = mergeSort(left,mid) + mergeSort(mid+1,right);
        int l = left, r = mid+1, k = 0;
        int[] temp = new int[right-left+1]; 
        while(l<=mid&&r<=right){
            if(nums[l]<=nums[r]){
                temp[k++] = nums[l++];
            }else{
                res = res + (mid-l+1);
                temp[k++] = nums[r++];
            }
        }
        while(l<=mid){
            temp[k++] = nums[l++];
        }while(r<=right){
            temp[k++] = nums[r++];
        }
        for(int i = 0;i<k;i++){
            nums[i+left] = temp[i];
        }
        return res;
    }
}

 4.链表的归并排序

import java.util.Scanner;

public class acw787_1 {
    static class  Node{
        int data;
        Node next;
        public Node(){};
        public Node(int data){
            this.data = data;
        }
    }
    public static Node mergeSortNode(Node head, Node tail){
        if(head == tail){
            head.next = null;
            return head;
        }
        Node p = head,q = head;

        while(q!=tail&&q.next!=tail){
            p =p.next;
            q = q.next.next;
        }
        Node t = p.next;

       Node newhead = mergeSortNode(head,p);
       Node newmid =  mergeSortNode(t,tail);
       Node temphead = new Node(-1), cur = temphead;
       while(newhead!=null&&newmid!=null) {
           if (newhead.data < newmid.data) {
               cur.next = newhead;
               newhead = newhead.next;
               cur = cur.next;
           } else {
               cur.next = newmid;
               newmid = newmid.next;
               cur = cur.next;
           }
       }
        while (newhead != null) {
            cur.next = newhead;
            newhead = newhead.next;
            cur = cur.next;
        }
        while (newmid != null) {
            cur.next = newmid;
            newmid = newmid.next;
            cur = cur.next;
        }
        cur.next = null;
       Node tt = temphead.next;
       while(tt!=null){
           System.out.print(tt.data + " ");
           tt = tt.next;
       }
        System.out.println();
        return temphead.next;

    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        Node dummy = new Node(-1), head = dummy;
        int n = in.nextInt();
        for(int i = 0;i<n;i++){
            int data = in.nextInt();
            Node node = new Node(data);
            head.next = node;
            head = head.next;
        }
        Node tail = dummy.next;
        while(tail.next!=null){
            tail = tail.next;
        }

       Node reshead =  mergeSortNode(dummy.next,tail);
        while(reshead!=null){
            System.out.print(reshead.data + " ");
            reshead = reshead.next;
        }
    }
}

  

 

posted @ 2023-01-12 15:06  lyjps  阅读(21)  评论(0)    收藏  举报