不同排序算法的时间复杂度,大家可以到wiki上查看它们的基本思想。
BinSort、Radix Sort和CountSort使用了不同的假设,所有,它们不是一般的排序方法。
下面是这些算法的具体实例
While analyzing source code of a large number of open source Java projects, I found Java developers frequently sort in two ways. One is using the sort()
method of Collections
or Arrays
, and the other is using sorted data structures, such as TreeMap
and TreeSet
.
Using sort() Method
If it is a collection, use Collections.sort() method.
// Collections.sortList |
If it is an array, use Arrays.sort() method.
// Arrays.sortObjectName[] arr = new ObjectName[10];Arrays.sort(arr, new Comparator |
This is very convenient if a collection or an array is already set up.
Using Sorted Data Structures
If it is a list or set, use TreeSet to sort.
// TreeSetSet |
If it is a map, use TreeMap to sort. TreeMap is sorted by key.
// TreeMap - using String.CASE_INSENSITIVE_ORDER which is a Comparator that orders Strings by compareToIgnoreCaseMap |
//TreeMap - In general, defined comparatorMap |
This approach is very useful, if you would do a lot of search operations for the collection. The sorted data structure will give time complexity of O(logn), which is lower than O(n).
Bad Practices
There are still bad practices, such as using self-defined sorting algorithm. Take the code below for example, not only the algorithm is not efficient, but also it is not readable. This happens a lot in different forms of variations.
double t;for (int i = 0; i < 2; i++) for (int j = i + 1; j < 3; j++) if (r[j] < r[i]) { t = r[i]; r[i] = r[j]; r[j] = t; } 归并排序
LeetCode – Sort List:
Sort a linked list in O(n log n) time using constant space complexity.
Keys for solving the problem
- Break the list to two in the middle
- Recursively sort the two sub lists
- Merge the two sub lists
This is my accepted answer for the problem.
package algorithm.sort; class ListNode { int val; ListNode next; ListNode(int x) { val = x; next = null; }} public class SortLinkedList { // merge sort public static ListNode mergeSortList(ListNode head) { if (head == null || head.next == null) return head; // count total number of elements int count = 0; ListNode p = head; while (p != null) { count++; p = p.next; } // break up to two list int middle = count / 2; ListNode l = head, r = null; ListNode p2 = head; int countHalf = 0; while (p2 != null) { countHalf++; ListNode next = p2.next; if (countHalf == middle) { p2.next = null; r = next; } p2 = next; } // now we have two parts l and r, recursively sort them ListNode h1 = mergeSortList(l); ListNode h2 = mergeSortList(r); // merge together ListNode merged = merge(h1, h2); return merged; } public static ListNode merge(ListNode l, ListNode r) { ListNode p1 = l; ListNode p2 = r; ListNode fakeHead = new ListNode(100); ListNode pNew = fakeHead; while (p1 != null || p2 != null) { if (p1 == null) { pNew.next = new ListNode(p2.val); p2 = p2.next; pNew = pNew.next; } else if (p2 == null) { pNew.next = new ListNode(p1.val); p1 = p1.next; pNew = pNew.next; } else { if (p1.val < p2.val) { // if(fakeHead) pNew.next = new ListNode(p1.val); p1 = p1.next; pNew = pNew.next; } else if (p1.val == p2.val) { pNew.next = new ListNode(p1.val); pNew.next.next = new ListNode(p1.val); pNew = pNew.next.next; p1 = p1.next; p2 = p2.next; } else { pNew.next = new ListNode(p2.val); p2 = p2.next; pNew = pNew.next; } } } // printList(fakeHead.next); return fakeHead.next; } public static void main(String[] args) { ListNode n1 = new ListNode(2); ListNode n2 = new ListNode(3); ListNode n3 = new ListNode(4); ListNode n4 = new ListNode(3); ListNode n5 = new ListNode(4); ListNode n6 = new ListNode(5); n1.next = n2; n2.next = n3; n3.next = n4; n4.next = n5; n5.next = n6; n1 = mergeSortList(n1); printList(n1); } public static void printList(ListNode x) { if(x != null){ System.out.print(x.val + " "); while (x.next != null) { System.out.print(x.next.val + " "); x = x.next; } System.out.println(); } }} |
Output:
Quicksort is a divide and conquer algorithm. It first divides a large list into two smaller sub-lists and then recursively sort the two sub-lists. If we want to sort an array without any extra space, Quicksort is a good option. On average, time complexity is O(n log(n)).
The basic step of sorting an array are as follows:
- Select a pivot, normally the middle one
- From both ends, swap elements and make all elements on the left less than the pivot and all elements on the right greater than the pivot
- Recursively sort left part and right part
package algorithm.sort; public class QuickSort { public static void main(String[] args) { int[] x = { 9, 2, 4, 7, 3, 7, 10 }; printArray(x); int low = 0; int high = x.length - 1; quickSort(x, low, high); printArray(x); } public static void quickSort(int[] arr, int low, int high) { if (arr == null || arr.length == 0) return; if (low >= high) return; //pick the pivot int middle = low + (high - low) / 2; int pivot = arr[middle]; //make left < pivot and right > pivot int i = low, j = high; while (i <= j) { while (arr[i] < pivot) { i++; } while (arr[j] > pivot) { j--; } if (i <= j) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; i++; j--; } } //recursively sort two sub parts if (low < j) quickSort(arr, low, j); if (high > i) quickSort(arr, i, high); } public static void printArray(int[] x) { for (int a : x) System.out.print(a + " "); System.out.println(); }} |
Output:
The mistake I made is selecting the middle element. The middle element is not (low+high)/2, but low + (high-low)/2. For other parts of the programs, just follow the algorithm.
插入排序
Insertion Sort List:
Sort a linked list using insertion sort.
This is my accepted answer for LeetCode problem – Sort a linked list using insertion sort in Java. It is a complete program.
Before coding for that, here is an example of insertion sort from . You can get an idea of what is insertion sort.
Code:
package algorithm.sort; class ListNode { int val; ListNode next; ListNode(int x) { val = x; next = null; }} public class SortLinkedList { public static ListNode insertionSortList(ListNode head) { if (head == null || head.next == null) return head; ListNode newHead = new ListNode(head.val); ListNode pointer = head.next; // loop through each element in the list while (pointer != null) { // insert this element to the new list ListNode innerPointer = newHead; ListNode next = pointer.next; if (pointer.val <= newHead.val) { ListNode oldHead = newHead; newHead = pointer; newHead.next = oldHead; } else { while (innerPointer.next != null) { if (pointer.val > innerPointer.val && pointer.val <= innerPointer.next.val) { ListNode oldNext = innerPointer.next; innerPointer.next = pointer; pointer.next = oldNext; } innerPointer = innerPointer.next; } if (innerPointer.next == null && pointer.val > innerPointer.val) { innerPointer.next = pointer; pointer.next = null; } } // finally pointer = next; } return newHead; } public static void main(String[] args) { ListNode n1 = new ListNode(2); ListNode n2 = new ListNode(3); ListNode n3 = new ListNode(4); ListNode n4 = new ListNode(3); ListNode n5 = new ListNode(4); ListNode n6 = new ListNode(5); n1.next = n2; n2.next = n3; n3.next = n4; n4.next = n5; n5.next = n6; n1 = insertionSortList(n1); printList(n1); } public static void printList(ListNode x) { if(x != null){ System.out.print(x.val + " "); while (x.next != null) { System.out.print(x.next.val + " "); x = x.next; } System.out.println(); } }} |
Output:
6.递归和迭代
下面通过一个例子来说明什么是递归。
问题:
这里有n个台阶,每次能爬1或2节,请问有多少种爬法?
步骤1:查找n和n-1之间的关系
为了获得n,这里有两种方法:一个是从第一节台阶到n-1或者从2到n-2。如果f(n)种爬法刚好是爬到n节,那么f(n)=f(n-1)+f(n-2)。
步骤2:确保开始条件是正确的
f(0) = 0; f(1) = 1;
1 2 3 4 5 | public static int f(int n){ if (n <= 2) return n; int x = f(n-1) + f(n-2); return x; } |
递归方法的时间复杂度指数为n,这里会有很多冗余计算。
1 2 3 4 | f(5) f(4) + f(3) f(3) + f(2) + f(2) + f(1) f(2) + f(1) + f(2) + f(2) + f(1) |
该递归可以很简单地转换为迭代。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | public static int f(int n) { if (n <= 2){ return n; } int first = 1, second = 2; int third = 0; for (int i = 3; i <= n; i++) { third = first + second; first = second; second = third; } return third; } |
1. Recursion
Consider the factorial function: n!=n*(n-1)*(n-2)*...*1
There are many ways to compute factorials. One way is that n! is equal to n*(n-1)!. Therefore the program can be directly written as:
Program 1:
int factorial (int n) { if (n == 1) { return 1; } else { return n*factorial(n-1); }} |
In order to run this program, the computer needs to build up a chain of multiplications: factorial(n) → factorial(n-1) → factorial(n-2) → … → factorial(1). Therefore, the computer has to keep track of the multiplications to be performed later on. This type of program, characterized by a chain of operations, is called recursion. Recursion can be further categorized into linear and tree recursion. When the amount of information needed to keep track of the chain of operations grows linearly with the input, the recursion is called linear recursion. The computation of n! is such a case, because the time required grows linearly with n. Another type of recursion, tree recursion, happens when the amount of information grows exponentially with the input. But we will leave it undiscussed here and go back shortly afterwards.
2. Iteration
A different perspective on computing factorials is by first multiplying 1 by 2, then multiplying the result by 3, then by 4, and so on until n. More formally, the program can use a counter that counts from 1 up to n and compute the product simultaneously until the counter exceeds n. Therefore the program can be written as:
Program 2:
int factorial (int n) { int product = 1; for(int i=2; i |
This program, by contrast to program 2, does not build a chain of multiplication. At each step, the computer only need to keep track of the current values of the product and i. This type of program is called iteration, whose state can be summarized by a fixed number of variables, a fixed rule that describes how the variables should be updated, and an end test that specifies conditions under which the process should terminate. Same as recursion, when the time required grows linearly with the input, we call the iteration linear recursion.
3. Recursion vs Iteration
Compared the two processes, we can find that they seem almost same, especially in term of mathematical function. They both require a number of steps proportional to n to compute n!. On the other hand, when we consider the running processes of the two programs, they evolve quite differently.
In the iterative case, the program variables provide a complete description of the state. If we stopped the computation in the middle, to resume it only need to supply the computer with all variables. However, in the recursive process, information is maintained by the computer, therefore “hidden” to the program. This makes it almost impossible to resume the program after stopping it.
4. Tree recursion
As described above, tree recursion happens when the amount of information grows exponentially with the input. For instance, consider the sequence of Fibonacci numbers defined as follows:
By the definition, Fibonacci numbers have the following sequence, where each number is the sum of the previous two: 0, 1, 1, 2, 3, 5, 8, 13, 21, ...
A recursive program can be immediately written as:
Program 3:
int fib (int n) { if (n == 0) { return 0; } else if (n == 1) { return 1; } else { return fib(n-1) + fib(n-2); }} |
Therefore, to compute fib(5), the program computes fib(4) and fib(3). To computer fib(4), it computes fib(3) and fib(2). Notice that the fib procedure calls itself twice at the last line. Two observations can be obtained from the definition and the program:
- The ith Fibonacci number Fib(i) is equal to Φi/√5 rounded to the nearest integer, which indicates that Fibonacci numbers grow exponentially.
- This is a bad way to compute Fibonacci numbers because it does redundant computation. Computing the running time of this procedure is beyond the scope of this article, but one can easily find that in books of algorithms, which is O(Φn). Thus, the program takes an amount of time that grows exponentially with the input.
On the other hand, we can also write the program in an iterative way for computing the Fibonacci numbers. Program 4 is a linear iteration. The difference in time required by Program 3 and 4 is enormous, even for small inputs.
Program 4:
int fib (int n) { int fib = 0; int a = 1; for(int i=0; i |
However, one should not think tree-recursive programs are useless. When we consider programs that operate on hierarchically data structures rather than numbers, tree-recursion is a natural and powerful tool. It can help us understand and design programs. Compared with Program 3 and 4, we can easily tell Program 3 is more straightforward, even if less efficient. After that, we can most likely reformulate the program into an iterative way.