Double heap median and C implementation

Source: Internet
Author: User

Heap

For more information about how to create and delete a heap, see http://blog.csdn.net/pngynghay/article/details/22101359.

Double heap Median

Algorithm Description:

1. Create two heaps (one small root heap and one large root heap). The heap size should be at least half of the given number of data (size + 1)/2, that is, rounded up;

2. Assume that the variable mid is used to save the median, set the first element, and assign the value to mid, which is used as the initial median;

3. Traverse each of the following data in sequence. If it is smaller than the mid value, insert a large root heap; otherwise, insert a small root heap;

4. If the number of data on the large and small root Stacks is different from 2, the mid will be inserted into the heap with a small number of elements, then, delete the root node from the heap with a large number of elements and assign the value to the mid node;

5. Repeat Steps 3 and 4 until all data traversal ends;

At this point, the mid saves a number, and the number saved in the two heaps constitutes a set of given data.

If the number of elements in the two heaps is equal, mid is the final Median. Otherwise, the root node element with more elements is the sum of mid and the average value is the final median.

Algorithm Implementation

The algorithm uses integers. Therefore, the final result is an integer. You can convert the return value to the floating point type to be more accurate.

uint32_t getmedian(uint32_t *array, uint32_t size){int i = 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);   return mid;}
Test procedure
#define NUM 10int main(){        uint32_t array[NUM] = {2,20,13,18,15,8,3,5,4,25};        getmedian(array, NUM);        printf("\n");        return 0;}
Test Results


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.