zoukankan      html  css  js  c++  java
  • 几种排序

    1.mergeSort O(nlogn)的时间复杂度,需要O(n)的额外空间。

     1 public void sortIntegers2(int[] A) {
     2         // Write your code here
     3         if (A == null || A.length == 0) {
     4             return ;
     5         }
     6         int[] temp = new int[A.length];
     7         mergeSort(A, 0, A.length - 1, temp);
     8     }
     9     
    10     private void mergeSort(int[] A, int start, int end, int[] temp) {
    11         if (start == end) {
    12             return ;
    13         }
    14         int mid = start + (end - start) / 2;
    15         mergeSort(A, start, mid, temp);
    16         mergeSort(A, mid + 1, end, temp);
    17         merge(A, start, mid, end, temp);
    18     }
    19     
    20     private void merge(int[] A, int start, int mid, int end, int[] temp) {
    21         int index = start;
    22         int left = start;
    23         int right = mid + 1;
    24         while(left <= mid && right <= end) {
    25             if (A[left] < A[right]) {
    26                 temp[index++] = A[left++];
    27             } else {
    28                 temp[index++] = A[right++];
    29             }
    30         }
    31         while (left <= mid) {
    32             temp[index++] = A[left++];
    33         }
    34         while (right <= end) {
    35             temp[index++] = A[right++];
    36         }
    37         for (int i = start; i <= end; i++) {
    38             A[i] = temp[i];
    39         }
    40     }
    View Code

    2.quickSort O(nlogn)的时间复杂度,需要O(1)的额外空间

     1 public void sortIntegers2(int[] A) {
     2         // Write your code here
     3         if (A == null || A.length == 0) {
     4             return ;
     5         }
     6         quickSort(A, 0, A.length - 1);
     7     }
     8     
     9     private void quickSort(int[] A, int start, int end) {
    10         if (start >= end) {
    11             return ;
    12         }
    13         int pivot = getPivot(A, start, end);
    14         int i = start;
    15         int j = end;
    16         while (i <= j) {
    17             while (i <= j && A[i] < pivot) {
    18                 i++;
    19             }
    20             while (i <= j && A[j] > pivot) {
    21                 j--;
    22             }
    23             if (i <= j) {
    24                 swap(A, i, j);
    25                 i++;
    26                 j--;
    27             }
    28         }
    29         quickSort(A, start, j);//Attention
    30         quickSort(A, i, end);//Attention
    31     }
    32     
    33     private int getPivot(int[] A, int start, int end) {
    34         Random random = new Random();
    35         int index = random.nextInt(end - start + 1);
    36         return A[start + index];
    37     }
    38     
    39     private void swap(int[] A, int i, int j) {
    40         int temp = A[i];
    41         A[i] = A[j];
    42         A[j] = temp;
    43     }
    View Code

    3.insertionSort O(n^2)的时间复杂度, 需要O(1)的额外空间。

     1 private void insertionSort(int[] A) {
     2         for (int p = 1; p < A.length; p++) {
     3             int j;
     4             int temp = A[p];
     5             for (j = p; j > 0 && A[j - 1] > temp ; j--) {
     6                     A[j] = A[j - 1];
     7             }
     8             A[j] = temp;
     9         }
    10     }
    View Code

    4.Insertion Sort List O(n^2)时间复杂度,需要O(1)的额外空间

    链表插入排序

    真是尴尬,开始时受到数组插入排序的影响,总想着从有序部分的后面开始寻找插入位置,还想着怎么搞一个指向前边的指针,定式思维的危害啊。。。

    其实从有序部分的前边开始寻找插入位置就可以了嘛

    需要注意的是:

    dummy.next 并没有指向head,有序部分的最后一个节点指向的是null,这就可以作为每轮插入的一个循环控制条件。

     1 public ListNode insertionSortList(ListNode head) {
     2         ListNode dummy = new ListNode(0);
     3         while (head != null) {
     4             ListNode node = dummy;
     5             while (node.next != null && node.next.val < head.val) {
     6                 node = node.next;
     7             }
     8             ListNode temp = head.next;
     9             head.next = node.next;
    10             node.next = head;
    11             head = temp;
    12         }
    13         return dummy.next;
    14     }
    View Code

    5.归并排序

     1 public ListNode sortList(ListNode head) {
     2         return mergeSort(head);
     3     }
     4     public ListNode mergeSort(ListNode head) {
     5         if (head == null || head.next == null) {
     6             return head;
     7         }
     8         ListNode middle = findMiddle(head);
     9         ListNode right = middle.next;
    10         middle.next = null;
    11         ListNode left = mergeSort(head);
    12         right = mergeSort(right);
    13         return merge(left, right);
    14     }
    15     public ListNode merge(ListNode left, ListNode right) {
    16         ListNode dummy = new ListNode(0);
    17         ListNode tail = dummy;
    18         while (left != null && right != null) {
    19             if (left.val < right.val) {
    20                 tail.next = left;
    21                 tail = left;
    22                 left = left.next;
    23             } else {
    24                 tail.next = right;
    25                 tail = right;
    26                 right = right.next;
    27             }
    28         }
    29         tail.next = left == null ? right : left;
    30         return dummy.next;
    31     } 
    32     public ListNode findMiddle(ListNode head) {
    33         ListNode right = head;
    34         ListNode dummy = new ListNode(0);
    35         dummy.next = head;
    36         ListNode left = dummy;
    37         while (right != null && right.next != null) {
    38             right = right.next.next;
    39             left = left.next;
    40         }
    41         return left;
    42     }
    View Code

    6.单项链表冒泡排序

    public static ListNode bubbleSort(ListNode head) {
            ListNode dummyHead = new ListNode(0);
            dummyHead.next = head;
            ListNode cur = head;
            int count = 0;
            while (cur != null) {
                count++;
                cur = cur.next;
            }
            ListNode prev = dummyHead;
            for (int i = count; i > 0; i--) {
                prev = dummyHead;
                cur = prev.next; // Attention!
                int j = 0;
                while (j < i - 1) {
                    if (cur.val > cur.next.val) {
                        ListNode temp = cur.next.next;
                        prev.next = cur.next;
                        cur.next.next = cur;
                        cur.next = temp;
                    }
                    prev = prev.next;
                    cur = prev.next;
                    j++;
                }
            }
            return dummyHead.next;
        }
    View Code
  • 相关阅读:
    < java.util >-- Set接口
    Codeforces 627 A. XOR Equation (数学)
    Codeforces 161 B. Discounts (贪心)
    Codeforces 161 D. Distance in Tree (树dp)
    HDU 5534 Partial Tree (完全背包变形)
    HDU 5927 Auxiliary Set (dfs)
    Codeforces 27E. Number With The Given Amount Of Divisors (暴力)
    lght oj 1257
    Codeforces 219D. Choosing Capital for Treeland (树dp)
    Codeforces 479E. Riding in a Lift (dp + 前缀和优化)
  • 原文地址:https://www.cnblogs.com/futurehau/p/5999444.html
Copyright © 2011-2022 走看看