Find the smallest number of K (update)

Source: Internet
Author: User

Looking for the largest number of K in programming-like beauty

Solution One:

The problem does not require the smallest number of K ordered, and did not require the final n-k number ordered. In this case, it is not necessary to sort all the elements. At this time, we thought of the choice or exchange sort, namely:

1, traversing n number, the first to traverse to the number of k deposited into the size of the array of k, assuming that they are the smallest number of k;
2, to this k number, using the choice or exchange sorting to find the maximum value of this k element Kmax (find the maximum need to traverse this K number, time complexity is O(k) );
3, continue to traverse the remaining number of n-k. Assuming that the value of the new element to be traversed is X, compare X with Kmax: If x < kmax , replace Kmax with X, and go back to the second step to find the largest element in the array of k elements Kmax '; if x >= kmax , continue traversing does not update the array.

The time taken for each traversal, update, or not to update an array is O(k) either O(0) . So the whole trip down, the time complexity of n*O(k)=O(n*k)

Solution Two: Fast selection algorithm

Quicksort can modified to solve the selection problem, which we had seen in chapters 1 and 6. Recall that by using a priority queue, we can find the kth largest (or smallest) element in O (n + k log n). For the special case of finding the median, this gives an O (n log n) algorithm.

Since We can sort the file in O (nlog N) time, one might expect to obtain a better time bound for selection. The algorithm we present to find the kth-smallest element in a set S are almost identical to quicksort. In fact, the first three steps is the same. We'll call this algorithm quickselect (called Quick Select). Let | si| Denote the number of elements in Si (make | Si| is the number of elements in the SI). The steps of Quickselect are ( quick selection, that is, the beauty of the above-mentioned book, Idea 4, steps below ):

1. If | s| = 1, then K = 1 and return the elements in S as the answer. If a cutoff for small files are being used and | s| <=cutoff, then sort S and return the kth smallest element.
2. Pick a pivot element, V (-S. (Select a PIVOT element V belongs to s)
3. Partition S-{v} into S1 and S2, as is done with quicksort.
(Divide the collection S-{v} into S1 and S2, as we did in the quick sort)

    4. If k <= | S1|, then the kth smallest element must is in S1. In this case, return Quickselect (S1, K). If k = 1 + | s1|, then the pivot is the kth smallest element and we can return it as the answer. Otherwise, the kth smallest element lies in S2, and it is the (K-| s1| -1) St smallest element in S2. We make a recursive call and return Quickselect (S2, K-| s1| -1).
(If k<=| s1|, then the smallest element of K must be in S1. In this case, return Quickselect (s1,k). If k=1+| s1|, then the pivot element is the K-min element, which is found and returned directly. Otherwise, this k smallest element is in S2, which is S2 (k-| s1|-1) The smallest element, we recursively call and return Quickselect (s2,k-| s1|-1)).

In contrast to Quicksort, Quickselect makes only one recursive call instead of. The worst case of Quickselect was identical to that of Quicksort and is O (N2). Intuitively, this was because Quicksort ' s worst case was when one of the S1 and S2 is empty; Thus, Quickselect (quick selection) is not really saving a recursive call. The average running time, however, is O (n) ( However, its average runtime is O (n). See, it is the average complexity of O (N) this sentence). The analysis was similar to Quicksort's and was left as a exercise.

The implementation of Quickselect is even simpler than the abstract description might imply. The code to does this shown in Figure 7.16. When the algorithm terminates, the kth smallest element was in position K. This destroys the original ordering; If this isn't desirable, then a copy must be made.

    • Select an element in S as the hub element V, divide the set s-{v} into S1 and S2, just like fast sorting
      • If k <= | s1|, then the smallest element of K must be in S1. In this case, return Quickselect (S1, K).
      • If k = 1 + | s1|, then the pivot element is the K-min element, which is found and returned directly.
      • Otherwise, the K-min element is in S2, which is the S2 (K-| s1| -1) Minimum element, we recursively call and return Quickselect (S2, K-| s1| -1).

The average run time for this algorithm is O (n).

The sample code is as follows:

1 //Quickselect Place K small elements in a[k-1]2 voidQuickselect (intA[],intKintLeftintRight )3 {4     intI, J;5     intpivot;6 7     if(Left + cutoff <=Right )8     {9Pivot =Median3 (A, left, right);Ten         //taking a three-digit median as a pivot element can largely avoid the worst case scenario Onei = left; j = Right-1; A          for( ; ; ) -         { -              while(a[++i] <pivot) { } the              while(a[--j] >pivot) { } -             if(I <j) -Swap (&a[i], &a[J]); -             Else +                  Break; -         } +         //Reset Pivot Element ASwap (&a[i], &a[right-1 ] );  at  -         if(k <=i) -Quickselect (A, k, left, I-1 ); -         Else if(k > i +1 ) -Quickselect (A, k, i +1, right); -     } in     Else   -Insertsort (A + left, Right-left +1 ); to}

Find the smallest number of K (update)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.