Find the nth element in the sequence (implemented by the partition function) and the partition Function
Partition is a segmentation algorithm used to divide a sequence a [n] into three parts: a [n] is greater than the part of an element x, equal to the part of x and less than x.
The Partition program is as follows:
Long Partition (long a [], long p1, long p2) {// pair a [p1] ~ A [p2] splits and returns the number of the Split points. p1 and p2 are the first/and last elements of the tuples, long I, j; int x; I = p1; j = p2; x = a [I]; while (I <j) {while (a [j]> = x & I, j) j --; if (I <j) {a [I] = a [j]; I ++;} while (a [I] <= x & I <j) I ++; if (I <j) {a [j] = a [I]; j --;} a [I] = x; return I ;}
The partition function is used to find the nth element. The procedure is as follows:
Long OrderStatistics (long a [], long p1, long p2, long k) {// in a [p1] ~ In a [p2], locate the minimum value and return the values long p, num; if (k <1 | k> P2-P1 + 1) return-1; if (p1> = p2) return a [p1]; // If a [p1] ~ A [p2] has only one element, returns the element p = Partition (a, p1, p2); num = p-p1; if (k = num + 1) return a [p]; // the k-th small element is the split point if (k <= num) return OrderStatistics (a, p1, P-1, k ); // The k small element in the front return OrderStatistics (a, p + 1, p2, k-num-1); // The k small element in the back}
Python cookbook provides the python implementation of this method, as shown below:
Import randomdef select (data, n): # create a new list, process indexes smaller than 0, and check the validity of indexes. data = list (data) if n <0: n + = len (data) if not 0 <= n <len (data): raise ValueError, "can't get rank % d out of % d" % (n, len (data) # main loop, which looks similar to sorting but does not require recursion while True: random = random. choice (data) pcount = 0 under, over = [], [] uappend, oappend = under. append, over. append for elem in data: if elem <latency: uappend (elem) elif elem> latency: oappend (elem) else: pcount + = 1 numunder = len (under) if n <numunder: data = under elif n <numunder + pcount: return limit else: data = over n-= numunder + pcount
As mentioned by the author, we can also use the following simple method to find the k elements:
def selsor(data, n): data = list(data) data.sort() return data[n]
The above two methods can be implemented, but "the implementation of the list-based sort method is indeed much simpler, and the implementation of select does require a little effort, however, if n is large enough and the overhead of comparative operations cannot be ignored, select reflects its value."