Question: There are a lot of unordered numbers. How to select the largest k number? Analysis: I have encountered this problem in programmer programming art before-the smallest k Number. The essence is the same. Here we will summarize: Idea 1: Use the fast-sort method to select an element in S as the pivot element v, and split the set S-{v} into S1 and S2, just like fast sorting
Question: There are a lot of unordered numbers. How to select the largest k number? Analysis: I have encountered this problem in programmer programming art before-the smallest k Number. The essence is the same. Here we will summarize: Idea 1: Use the fast-sort method to select an element in S as the pivot element v, and split the set S-{v} into S1 and S2, just like fast sorting
Question:
There are many unordered numbers. How to select the maximum k number from them?
Question Analysis:
I have encountered such a problem in "Programmer programming art"-the minimum k number. The essence is the same.
Here is a summary:
Idea 1: Use a method similar to Quick Sort
- Select an element in S as the pivot element v, and split the set S-{v} into S1 and S2, just as in fast sorting.
- If k <= | S1 |, the minimum k element must be in S1. In this case, QuickSelect (S1, k) is returned ).
- If k = 1 + | S1 |, the pivot element is the minimum element of k, that is, the smallest element of k.
- Otherwise, the k-th smallest element is in S2, that is, the (k-| S1 |-1) smallest element in S2. We call it recursively and return QuickSelect (S2, k-| S1 |-1 ).
The average running time of this algorithm is O (n ).
Simplified Version (select median among the three elements)
// QuickSelect place the small k element in a [k-1] void QuickSelect (int a [], int k, int left, int right) {int I, j; int limit; if (left + cutoff <= right) {centers = median3 (a, left, right); // use the median value as the hub element, to a large extent, we can avoid the worst case: I = left; j = right-1; for (;) {while (a [++ I] <distinct) {} while (a [-- j]> else) {} if (I <j) swap (& a [I], & a [j]); else break ;} // reset the hub meta swap (& a [I], & a [right-1]); if (k <= I) QuickSelect (a, k, left, i-1); else if (k> I + 1) QuickSelect (a, k, I + 1, right);} else InsertSort (a + left, right-left + 1 );}
Randomization version:
// Random Partitionint RandomInRange(int min, int max){ int random = rand() % (max - min + 1) + min; return random;}void Swap(int* num1, int* num2){ int temp = *num1; *num1 = *num2; *num2 = temp;}int Partition(int data[], int length, int start, int end){ if(data == NULL || length <= 0 || start < 0 || end >= length) throw new std::exception("Invalid Parameters"); int index = RandomInRange(start, end); Swap(&data[index], &data[end]); int small = start - 1; for(index = start; index < end; ++ index) { if(data[index] < data[end]) { ++ small; if(small != index) Swap(&data[index], &data[small]); } } ++ small; Swap(&data[small], &data[end]); return small;}int MoreThanHalfNum_Solution1(int* numbers, int length){ if(CheckInvalidArray(numbers, length)) return 0; int middle = length >> 1; int start = 0; int end = length - 1; int index = Partition(numbers, length, start, end); while(index != middle) { if(index > middle) { end = index - 1; index = Partition(numbers, length, start, end); } else { start = index + 1; index = Partition(numbers, length, start, end); } } int result = numbers[middle]; if(!CheckMoreThanHalf(numbers, length, result)) result = 0; return result;}
Thought 2:
The heap sorting method sorts the first k first and then traverses the whole sequence. This method is especially suitable for a large amount of data.
If (X> h [0]) {h [0] = X; p = 0; while (p <K) {q = 2 * p + 1; if (q> = K) break; if (q <K-1) & (h [q + 1]
Thought 3:
Another method that is relatively limited: You can use the Count sorting method. When all N numbers are positive and the change range is not large, we can set an array to count the number of occurrences of each data. Then, traverse the array and find the maximum k pieces of data.
for(sumcount = 0,v = MAXN - 1;v >= 0;v--){ sumcount += count[v]; if(sumcount >= k) break;}return v;
Expansion:
After all, for big data, we usually do not know how to make our cognition wider and process big data.
If we cannot ensure that all numbers are positive and the conversion range is not large, we can also use Idea 3 to group and process them:
Assume that the maximum value of N is max and the minimum value is min. We can divide [min, max] into M regions, and the span of each small area is (max-min)/M. Then we can scan all the data and count the data contained in each region. Then, locate the region in which the big data of the local data resides. Then, block the local area.