Overview
in the algorithm series (four) sorting algorithm medium--merge sort and quick sort article, we introduce merge sort and quick sort, in the worst case, the time complexity of the quickest sorting algorithm is O (NLOGN), is there a better algorithm? So far, there are no special rules,O (nlogn) is already the best sorting algorithm, that is to say, the general ranking algorithm of the time complexity of the lower bound is O (Nlogn). If you limit some rules, you can break the nether. Here is an algorithm that can be used to sort the array in O (n) time.
sorting-based rulesbased on what rules can we break the lower bound of the sort? We need to analyze the time spent on sorting. Sorting needs to be traversed, compared, exchanged. Can you omit some of these steps? This is the rule to define, reducing the sort steps by rules. Here is one of the simplest examples. a set of elements to be sorted is only 1 and 2, and there are no other values to sort the set of numbers. Enter A0,a1,a2,a3 ... An-1,ai is 1 or 2 .Sort Steps1, make k=02, make I from 0 to n-1 value, if a[i]=1,k self-increase 13, make I from 0 to k-1 value, the A[i] assigned to 14, let I from K to n-1, the value of A[i] assigned to 2so we finished the sort, and it took the time to O (n)The algorithms we talked about were all based on the comparison of element pairs to determine the order, which sort is called the comparison sort. General comparison sort, Universal Nether is O (NLOGN) A Simple example of what has just been said is a simple counting sort. Here is a detailed explanationuse cardinal sort to go beyond sort lower boundsSimple example each element has only two possible values, expand, if each element has m different values, as long as the value is an integer within m consecutive integers, the algorithm is universal. first, by calculating how many elements of the sort key are equal to a certain value, you can then even make out how many elements of the sort key are less than each possible sort. Basic IdeasThe basic idea of counting sorting is to determine the number of elements in the sequence that have a value less than x for each element in the given input sequence (this is not a comparison of the size of each element, but a count of the value of the element and the summation of the count value). Once you have this information, you can store the x directly in the correct position of the final output sequence.
detailed description of the counting sorting algorithmthe algorithm requires three basic methodscount-key-equal (a,n,m)enter an array of a,n number of elements in array avalue range of elements in M array aoutput An array of equal[0......m], which is equal[j] equals the number of elements of element value J in array a1. Create a new array equal[0......m]2, make equal array each element is 03, I from 0 to n-1 value, each time the value of Equal[a[i]] is increased by 14. Return to equalcount-key-less (equal,m)Enter the value of the Count-key-equal method corresponding to the value equal,moutput An array of less[0......m],less[j]=equal[0]+equal[1]+......+equal[j-1]1. Create a new array less[0...m]2, make less[0]=03, J from 1 to M,less[j]=less[j-1]+equal[j-1] (this is a common iterative algorithm)4. Return Lessrearrange (a,less,n,m)Enter the a,less,n,m for the Count-key-equal count-key-less methodThe output array b,b contains all the elements in a, and is already sorted .1. Create a new array b[0...n-1],next[0.....m]2, J from 0 to M in turn to take valuemake next[j]=less[j]+13, let I from 0 to n-1 in turn valuekey=a[i];index=next[key],b[index]=a[i],next[key]++4. Return array BCode Implementationlogical integration, the basic idea is the same
Package com.algorithm.sort;/** * Count sort * * @author Chao * */public class Countsort {public static void main (string[] Ar GS) {int[] num = {1, 1};sort (num), for (int i = 0; i < num.length; i++) System.out.print (Num[i] + "");} /** * Count Sort * * @param num */public static void sort (int[] num) {int len = num.length;int[] orign = new Int[len];int ma x = 0;//We sort only positive integers for (int i = 0; i < len; i++) {Orign[i] = num[i];if (Num[i] > max) {max = Num[i];}} max = max + 1;int[] count = new Int[max];for (int i = 0; i < max; i++) {count[i] = 0;} for (int i = 0; i < len; i++) {count[num[i]]++;} int T1, t2;t1 = count[1];count[0] = count[1] = 0;for (int i = 2; i < Max; i++) {t2 = count[i];count[i] = T1 + count[i- 1];T1 = T2;} int key, index;for (int i = 0; i < len; i++) {key = Orign[i];index = Count[key];num[index] = orign[i];count[key]++;}}}
Analysis of Complexity The complexity of counting sorting is O (n), but there is a space cost, and if the maximum number is large, the space cost is very large.
There is also a sort of order called cardinality, which is based on the order of counts, with constraints, time complexity O (n) Bucket Sort, cardinal sort is similar to count sort, no longer detailed, heap sort (very common, will be explained in tree algorithm analysis)code implementation can look at GitHub, address Https://github.com/robertjc/simplealgorithmGitHub code is also constantly improving, some places may have problems, but also please more advice
Welcome to scan QR Code, follow public account
Algorithm series (four) ranking algorithm next article--how to go beyond the lower bounds of ranking algorithm