"Data structure" finds the largest top K data in n data (sorted by heap)

Source: Internet
Author: User

Let us, for example, choose the first 100 largest data from 100 million numbers.


First we analyze: since we want to select the top 100 of the largest data, we build a heap of size 100 (when the heap is built to find the maximum heap of rules established, that is, each root node is greater than its child node), and then the rest of the remaining data if required to insert the heap, non-conforming directly discard the data.


Let's consider now: Is it OK to choose the data structure of the largest heap or the smallest heap?


Analysis:

If we choose the largest heap, the heap top is the maximum value of the heap, we consider that since the selection of the top 100 from 100 million number of the largest data, we are in the construction of the heap, we have considered the maximum heap characteristics, then the largest data must be at the top of it. If unfortunately, I started in the first 100 data already have the maximum value of these 10,000 data, that for the remaining 10000-100 of the elements of my back to go into the heap is not into the!!! So, choosing the largest heap from 100 million numbers to select the top 100 largest data can only find one, not 100.


If you choose the smallest heap of data structure to solve, the top is the minimum value, again encountered larger than its value, you can go into the heap, into the heap after the heap adjustment, the small value pass off. So we can pick out the largest of the first K data.


Code implementation:

#define  _CRT_SECURE_NO_WARNINGS 1#include<iostream>using namespace std; #include < Assert.h>void adjustdown (int* a, int parent, int size) {     int child = 2 * parent + 1;    while  (Child  < size)     {        if  (child &NBSP;+&NBSP;1&NBSP;&LT;&NBSP;SIZE&NBSP;&AMP;&AMP;&NBSP;A[CHILD]&NBSP;&GT;&NBSP;A[CHILD&NBSP;+&NBSP;1])          {             child++;        }         if  (A[parent]>a[child])         {             swap (A[parent], a[child]);             parent = child;             child = 2 * parent + 1;        }         else        {             break;         }    }}void print (int* a, int size) {     cout <<  "Top K Max data:"  << endl;    for  (int i  = 0; i < size; i++)     {         cout << a[i] <<  "  ";     }     cout << endl;} Void heapset (int*a,int n,int k) {    assert (a);      assert (k > 0);     int* arr = new int[k ];    //Save the first K data     for  (int i = 0; i  < k; i++)     {        arr[i] =  a[i];    }    //Building     for  (int i  = 0; i <  (k - 2)  / 2; i++)     {         adjustdown (arr,i,k);    }     //to the remaining n-k elements compare size     for  (int i = k; i <  n; i++)     {        if  (Arr[0]<a[i])         {             arr[0] =&nbSp;a[i];            adjustdown (Arr, 0, K );         }    }    print (arr, K);     delete[] arr;} Void test () {    int arr[] = { 12, 2, 10, 4, 6 , 8, 54, 67, 25, 178 };    int k = 5;     heapset (arr, sizeof (arr)  / sizeof (Arr[0]), k);} Int main () {    test ();     system ("Pause");     return 0;}


It can be seen that the time complexity is: K + (K-2)/2*lgn+ (n-k) *lgn--> O (N)

The spatial complexity is: K-->o (1).

This article is from "Han Jing's Blog", please make sure to keep this source http://10740184.blog.51cto.com/10730184/1768075

"Data structure" finds the largest top K data in n data (sorted by heap)

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.