Find the element in the array I small

Source: Internet
Author: User

Find and output the small element in the array I, such a problem we can first sort the arrays, and then output the corresponding small I-element; There is another method, a sorting algorithm to solve the selection problem, the algorithm is based on the fast sorting algorithm, and the same as the fast sort, we still divide the input array, But unlike a quick sort, a quick sort recursively handles both sides of the partition, and the Select method selects only the side of the partition. This difference is reflected in the performance analysis: the desired run time for the fast sort is O (Nlog (n)), and the select expected run time for the selection is O (n).


1, the array is sorted, and then enter the small I element, where the sorting algorithm with the insertion sort.

#include <iostream>using namespace Std;int select (int a[], int n, int i); int main (void) {    int a[5] = {4, 67, 8, 2 , 3};    Cout<<select (A, 5, 4) <<endl;    return 0;} static void Insertsort (int a[], int n) {    int i, J;    for (i = 1; i < n; i++)    {        int x = a[i];        for (j = i; J >= 0 && a[j-1] > x; j--)            a[j] = a[j-1];        A[J] = x;    }} int select (int a[], int n, int i) {    insertsort (A, n);    return a[i-1];}

2, recursive to select the element in the array I small elements, the Select function runs as follows: Just start to check the basic situation of recursion, when a[] contains only one element, I must be equal to 1, return directly. In other cases, the partition function is called, dividing the array into two sub-arrays (possibly empty), A[left. J-1] and A[j+1..right], making a[left. Each element in J-1] is less than or equal to A[j], and A[j] is less than each element in A[j+1..right]. As with the quick sort, we call a[j] The main element, and then the A[left in the Subarray. J] The number of elements in K, that is, the number of elements in the lower section of the Division plus 1. Then check whether A[j] is the small element of I, if it is next to return directly, otherwise determine the small element I fall in the a[left. J-1] and A[j+1..right] two sub-arrays. If i<k, the element to be found falls in the lower section of the partition, then the recursive lookup continues in the sub-array of the lower region, and if i>k, the element to be found falls in the high area. We already know that there are k values less than aleft. Right] in the small element of I, namely A[left: J], so the element we're looking for is bound to be the small i-k element in A[j+1..right], and then the recursive lookup.

#include <iostream>using namespace Std;int select (int a[], int left, int. right, int i); int main (void) {    int a[5] = {4, 8, 2, 3};    Cout<<select (A, 0, 4, 3) <<endl;    return 0;} static void Swap (int &a, int &b) {    int t = A;    A = b;    b = t;} static int Partition (int a[], int left, int. right) {    int T, I, J;    t = a[right];    i = left;    for (j = left; J < right; J + +)    {        if (A[j] < T)            Swap (a[i++], a[j]);    }    Swap (A[i], a[right]);    return i;} int select (int a[], int left, int. right, int i) {    if (left = right)        return a[left];    Int J = Partition (A, left, right);    int k = j-left + 1;    if (k = = i)        return a[j];    else if (I < k)        return Select (A, left, j-1, i);    else        return Select (A, j+1, right, i-k);}


Resources:

1, "Introduction to Algorithms" (third edition) 9th-median and sequential statistics



Find the element in the array I small

Related Article

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.