Double heap to find the median number

Source: Internet
Author: User


Dynamic creation and deletion of heaps can be referred to, which is not mentioned here.

double heap to find the median number

Algorithm Description:

1, create two heaps (a small Gan, a large root heap), the heap size is at least half of the given number of data, rounding up;

2, the assumption that the variable mid is used to save the median, the first element, assigned to mid, that is, as the initial median;

3, sequentially traverse each subsequent data, if smaller than mid, then insert Dagen; otherwise insert small Gan;

4, if the number of data on the Dagen and the small root is 2, the mid is inserted into a heap with fewer elements, then the root node is removed from the heap with a large number of elements, and the node is assigned a value to mid;

5. Repeat steps 3 and 4 until all data traversal is complete;

At this point, mid saves a number, plus the number saved in two heaps, which makes up a collection of the given data.

If the number of elements in the two heap is equal, mid is the final median, otherwise, the root node element of the heap with more elements is the median of the mid and the average of the middle.

Algorithm implementation

The implementation of the algorithm takes an integer, so the final result takes an integer, which converts the return value to a floating-point type and is more accurate.

uint32_t Getmiddle (uint32_t*array, uint32_t size) {  inti =0, Minelem =0, Maxelem =0;  uint32_t mid = array[0];  uint32_t heapsize = (size+1)/2;  heap_t*minheap= Heap_malloc (heapsize);  heap_t*maxheap= Heap_malloc (heapsize);   for(i =1; i < size; i++)  {    if(Array[i] < mid)    {      Maxheap_insert (Maxheap, array[i]);      maxelem++;    }    Else    {      Minheap_insert (Minheap, array[i]);      minelem++;    }    if(Minelem-maxelem >1)    {      Maxheap_insert (Maxheap, mid);      MID = minheap->element[0];      Minheap_delete (MINHEAP);      maxelem++;      minelem--;    }    if(Maxelem-minelem >1)    {      Minheap_insert (Minheap, mid);      MID = maxheap->element[0];      Maxheap_delete (MAXHEAP);      minelem++;      maxelem--;    }  }  printf("\nmaxelem = %d, Minelem = %d, Pre_mid = %d\ n", Maxelem, Minelem,mid);  if(Minelem > Maxelem)  {    printf("\nmid[ %d ] = (Mid [ %d ]+minheap->element[0][ %d ])/2 = %d\ n", Mid, Mid,      minheap->element[0], (mid+minheap->element[0])/2);    Mid = (mid+minheap->element[0])/2;  }  if(Maxelem < Maxelem)  {      printf("\nmid[ %d ] = (Mid [ %d ]+maxheap->element[0][ %d ])/2 = %d\ n", Mid, Mid,      minheap->element[0], (mid+maxheap->element[0])/2);    Mid = (mid+maxheap->element[0])/2;  }      Heap_free (MINHEAP);  Heap_free (MAXHEAP);  returnMid;}
Test Program
intMainintargcChar*argv[]) {intnum =Ten; uint32_tArray[num] = {2, -, -, -, the,8,3,5,4, -}; Getmiddle (Array, num); printf"\ n");return 0;}
Test Results

Double heap to find the median number

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: 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.