Median in data stream __ median in the data stream

Source: Internet
Author: User
Tags data structures int size throw exception
question: How to get the median number in a data stream. If you read an unusually number of values from a data stream, the median is the number in the middle after all the values are sorted. If an even number of values is read in the stream, the median is the average of two digits in the middle after all the values are sorted. Analysis: The following summary uses the time complexity of not-sorted arrays, sorted arrays, sorted lists, binary search trees, AVL numbers, maximum heaps, and the smallest stacks of several different data structures.
Train of thought: Consider using the heap implementation, consider the data sequence from the middle start into two parts, the left part using Dagen, the right part of the small Gan storage. For each data traversal, counter count increases by 1, inserting the data into a small Gan when count is even, and inserting the data into the large heap when count is odd. When all data traversal inserts are complete (time complexity is O (logn), if Count is finally an even number, the median is half the size of the top element of the heap heap and the top element of the small root heap heap, and if Count is the last odd, the median is the small root heap heap top element.

The detailed implementation code is as follows:
Template<typename t> class Dynamicarray {public:void Insert (T num) {if ((min.size () + max.size ()) & 1) = = 0) {if (max.size () > 0 && num < max[0]) {MAX.P
                Ush_back (num);

                Push_heap (Max.begin (), Max.end (), less<t> ());

                num = max[0];
                Pop_heap (Max.begin (), Max.end (), less<t> ());
            Max.pop_back ();
            } min.push_back (num);
        Push_heap (Min.begin (), Min.end (), greater<t> ()); else {if (min.size () > 0 && min[0] < num) {MIN.PU
                Sh_back (num);

                Push_heap (Min.begin (), Min.end (), greater<t> ());

                num = min[0];
                Pop_heap (Min.begin (), Min.end (), greater<t> ());
            Min.pop_back ();
            } max.push_back (num); Push_heap (Max.begin (), Max.end (), less<t> ());
        }///Get the median of the data stream T Getmedian () {int size = Min.size () + max.size ();

        if (size = = 0) Throw exception ("No numbers are available");
        T median = 0;
        if (size & 1) = = 1) median = min[0];

        else median = (Min[0] + max[0])/2;
    return median;
    } private:vector<t> min;
Vector<t> Max;

}; ==================== test Code ==================== void Test (char* testname, dynamicarray<double>&

    Numbers, double expected {if (testname!= NULL) printf ("%s begins:", testname); if (ABS (numbers).
    Getmedian ()-expected) < 0.0000001) printf ("passed.\n");
else printf ("failed.\n");

    int main (int argc, char* argv[]) {dynamicarray<double> numbers;
    printf ("Test1 begins:"); try {numbers.
        Getmedian ();
    printf ("failed.\n");
      catch (Exception e) {  printf ("passed.\n"); } numbers.
    Insert (5);

    Test ("Test2", numbers, 5); Numbers.
    Insert (2);

    Test ("Test3", numbers, 3.5); Numbers.
    Insert (3);

    Test ("Test4", numbers, 3); Numbers.
    Insert (4);

    Test ("Test6", numbers, 3.5); Numbers.
    Insert (1);

    Test ("Test5", numbers, 3); Numbers.
    Insert (6);

    Test ("Test7", numbers, 3.5); Numbers.
    Insert (7);

    Test ("Test8", numbers, 4); Numbers.
    Insert (0);

    Test ("Test9", numbers, 3.5); Numbers.
    Insert (8);

	Test ("Test10", numbers, 4);
return 0;

 }



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.