CC150 20.9

Source: Internet
Author: User

20.9 Numbers is randomly generated and passed to a method. Write a program to find and maintain the median value as new values is generated.

Class mediannum{  // o (n)   void insert (int n)   {     if  (size == 0)     {      head  = new node (n);      media = head;     }    else if  (N < head.num)     {       node newhead = new node (n);       newHead.next = head;      head.pre = newHead;       head = newHead;      median =  median.pre;    }    else    {       // find the position      node n =  head;   &nbSp;  boolean passmedian = false;      node node  = new node (i);      while  (n.next != null)        {        if  (n.next.num >  n)         {           node.next = n.next;           n.next.pre = node;          n.next =  node;          node.pre = n;           if  (Passmedian)            {            median =  median.next;          }          else           {             median = median.pre;          }           break;        }         passMedian = n == median;       }            // at  the end      n.next = node;       node.pre = n;      median = median.pre;     }        size++;  }    //  o (1)   int meDium ()   {    if  (size == 0)        throw exception;          if  (size % 2 &NBSP;==&NBSP;1)  // odd    {      return  median.num;    }    else // even    {        return  ( median.num + median.next.nu )  / 2;    }  }    private class node   {    int num;    Node next;     Node.pre;  }     private Node head = null;   private node median = null;  private int size = 0;}  what about using heap.class mediannum{  private size = 0;    heap  minheap; // all nums > median  heap maxheap; // all  Nums <= median    // o (log n)   void insert (int  N)   {    if (size == 0)     {       maxheap.inseart (n);    }    else if  (size  == 1 && n >= maxheap.top ())     {       minheap.inseart (n);    }    else if  (N <= maxheap.top ())     {      minheap.inseart ( Maxheap.removetop ());       maxheap.inseart (n);    }     else     {      maxheap.inseart (Minheap.removetop ());       minheap.inseart (n);    }         size++;  }    // o (1)   in median ()   {     if  (size == 0)       throw exception         if  (size % 2 == 1)     {       return maxheap.top ();    }     else    {      return  (Maxheap.top ()  +  Minheap.top ())  / 2;    }  }}


CC150 20.9

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.