Leetcode排序題

88. Merge Sorted Array
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.

Note:

The number of elements initialized in nums1 and nums2 are m and n respectively.
You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional elements from nums2.
Example:

Input:
nums1 = [1,2,3,0,0,0], m = 3
nums2 = [2,5,6], n = 3

Output: [1,2,2,3,5,6]
給兩個有序的數組 A 和 B,把 B 合併到 A,變成一個數組,假定 A 有足夠的空間。

JAVA

class Solution {
    public void merge(int A[] , int m, int B[] , int n) {
        int i = m - 1;
        int j = n - 1;
        int k = m + n - 1;
        while (k >= 0) {
        if (j < 0 || (i >= 0 && A[i] > B[j]))
            A[k--] = A[i--];
        else
            A[k--] = B[j--];
      }
   }
}

C++

class Solution {
public:
    void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
       if(n==0)
       {
           return;
       }
        int index=m+n-1;
        int i=m-1;
        int j=n-1;
        while(i>=0 && j>=0)
        {
            if(nums1[i]>nums2[j])
            {
                nums1[index--]=nums1[i--];
            }
            else
            {
                nums1[index--]=nums2[j--];
            }
        }
        if(i==-1)
        {
            while(j>=0)
            {
                nums1[j]=nums2[j];
                j--;
            }
        }
    }
};

147. Insertion Sort List
Sort a linked list using insertion sort.
在這裏插入圖片描述
A graphical example of insertion sort. The partial sorted list (black) initially contains only the first element in the list.
With each iteration one element (red) is removed from the input data and inserted in-place into the sorted list
Example 1:

Input: 4->2->1->3
Output: 1->2->3->4
Example 2:

Input: -1->5->3->4->0
Output: -1->0->3->4->5

用插入排序方法對鏈表進行排序。
JAVA

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode insertionSortList(ListNode head) {
        ListNode dummy=new ListNode(0);
        ListNode pre=dummy;
        ListNode cur=head;
       while (cur != null) {
			pre = dummy;
			// 從僞頭結點的下一個結點開始,(注意判空)
			// 讓pre指向小於cur.val的最大結點(升序)
			while (pre.next != null && pre.next.val < cur.val) {
			pre = pre.next;
        }
        // next暫存cur的下一個結點
			ListNode next = cur.next;
			// 把cur連接在pre後面
			// cur.next指向pre.next
			cur.next = pre.next;
			pre.next = cur;
			cur = next;
    }
    return dummy.next;
   }
}

148. Sort List

Sort a linked list in O(n log n) time using constant space complexity.

Example 1:

Input: 4->2->1->3
Output: 1->2->3->4
Example 2:

Input: -1->5->3->4->0
Output: -1->0->3->4->5
要求:1)時間複雜度爲O(nlogn);2)空間複雜度爲常數,即不能增設額外的空間。
歸併排序
JAVA

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode sortList(ListNode head) {
        if(head==null || head.next==null)
            return head;
        //使用快慢指針查找中間結點
        ListNode fast=head;
        ListNode slow=head;
        while(fast.next!=null)
        {
            fast=fast.next.next;
            //讓show少走一步,結點數目均勻
            if(fast==null)
                break;
            slow=slow.next;
        }
        ListNode right=slow.next;
        //注意斷裂
        slow.next=null;
        ListNode left=sortList(head);
        right=sortList(right);
        return mergeTwoLists(left,right);
    }
    //遞歸實現鏈表排序
    public ListNode mergeTwoLists(ListNode l1,ListNode l2)
    {
        ListNode res=null;
        if(l1==null)
            return l2;
        if(l2==null)
            return l1;
        if(l1.val<=l2.val)
        {
            res=l1;
            l1.next=mergeTwoLists(l1.next,l2);
        }
        else
        {
            res=l2;
            l2.next=mergeTwoLists(l1,l2.next);
        }
        return res;
    }
}

912. Sort an Array
Given an array of integers nums, sort the array in ascending order.

Example 1:

Input: [5,2,3,1]
Output: [1,2,3,5]
Example 2:

Input: [5,1,1,2,0,0]
Output: [0,0,1,1,2,5]
快速排序 升序
JAVA

class Solution {
    
    public int[] sortArray(int[] nums) {
        shuffle(nums);//避免最壞情況
        sortArray(nums,0,nums.length-1);
        return nums;
    }
    
    private void sortArray(int[] nums,int low,int high){
        if(low>=high) return;
        int j = patrition(nums,low,high);//nums[j]已經在合適的位置上了
        sortArray(nums,low,j-1);
        sortArray(nums,j+1,high);
    }
    
    private int patrition(int[] nums,int low,int high){
        int i = low;
        int j = high+1;
        while(true){
            while(nums[++i]<nums[low]){
                if(i==high) break;
            }
            while(nums[--j]>nums[low]){
 
            }
 
            if(i>=j) break;
 
            swap(nums,i,j);   
        }
        swap(nums,low,j);
        return j;
    }
    
    private void shuffle(int[] nums){
        Random random = new Random();
        for(int i = 1;i<nums.length;i++){
            int r = random.nextInt(i+1);
            swap(nums,r,i);
        }
    }
    
    private void swap(int[] nums,int i ,int j){
        int t = nums[i];
        nums[i] = nums[j];
        nums[j] = t;
    }
    
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章