Big Data applications for heap sequencing and heap

Source: Internet
Author: User

This exercise is the big data application of heap sorting and heap
The time complexity of heap sorting is O (n)
Heap of big data applications should choose a small heap for processing
But when the data is more than 100000, the speed is significantly slower, it may be the time to build a small heap slow "" "There is no better way



#include <iostream>
#include <vector>
#include <time.h>
using namespace Std;


//........................ The following is a heap sort ....... ..... ...............
void Adjustdowngreater (vector<int>& h,size_t size)//Build Large heap
{
if (size <= 1)//note parameter detection ah ....
{
Return
}
for (int parent = (SIZE-1-1)/2; parent >= 0; parent--)//experience: You can write the inner loop first, then the outer loop
{
while (parent <= size-1)
{
int Leftcchild = 2 * parent + 1;

if (Leftcchild + 1 <= size-1 && H[leftcchild] < H[leftcchild + 1])
{
leftcchild++;
}

if (leftcchild <= size-1 && h[parent] < H[leftcchild])
{
Swap (H[parent], h[leftcchild]);
parent = Leftcchild;
}
Else
{
Break
}
}
}
}

void Heapsort (vector<int>& h,size_t size)
{
while (Size > 1)
{
Swap (h[0], h[size-1]);
size--;

Adjustdowngreater (h,size);
}

}

void Print (vector<int>& h,size_t size)
{
for (int i = 0; i < size; i++)
{
cout << h[i]<< "";
}
}

void Test1 ()
{
int arr[] = {10, 16, 18, 12, 11, 13, 15, 17, 14, 19};

Vector<int> h;
for (int i = 0; i <; i++)
{
H.push_back (Arr[i]);
}

Adjustdowngreater (H, 10);
Heapsort (H, 10);
Print (H, 10);
}


//.................................. The following is a heap big data application ....... .....


The largest number of the top k from n data is ******* "small heap" **********************
The reason for choosing a small heap: Use an array to store the number of K arr[k] If the first number you choose is the biggest one, then you won't be able to get arr in the back.


const int N = 100000; When the speed of more than 100000 is significantly slower "there is no better way" "
const int K = 10;

void GetData (vector<int>& h)
{
Srand (Time (0));
for (int i = 0; i < N; i++)
{
H.push_back (rand ()% N + 1);
}
}

void Adjustdownless (vector<int>& h, size_t size)//Build small heap
{
if (size <= 1)
{
Return
}
for (int parent = (SIZE-1-1)/2; parent >= 0; parent--)
{
while (parent <= size-1)
{
int Leftcchild = 2 * parent + 1;

if (Leftcchild + 1 <= size-1 && H[leftcchild] > h[leftcchild + 1])
{
leftcchild++;
}

if (leftcchild <= size-1 && h[parent] > H[leftcchild])
{
Swap (H[parent], h[leftcchild]);
parent = Leftcchild;
}
Else
{
Break
}
}
}
}

void Getgreatestk (vector<int>& H, vector<int>& greatest)
{
Adjustdownless (H, N);

for (int i = 0; i < K; i++)//Put the front K smaller push in
{
Greatest.push_back (H[i]);
}

for (int index = K; index < N; index++)
{
Adjustdownless (Greatest, K); In order to get the smallest number

if (H[index] > Greatest[0])//Replace the smallest number
{
Greatest[0] = H[index];
}
}
}

void Print (vector<int>& h)
{
for (int i = 0; i < K; i++)
{
cout << h[i]<< "";
}
}

void Test2 ()
{
Vector<int> h;
Vector<int> greatest;
GetData (h);
GETGREATESTK (H, greatest);
Print (greatest);
}


int main ()
{
Test1 ();
Test2 ();

return 0;
}650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/7E/D8/wKiom1cKHXyj8EqyAAA0CYgicTU561.png "title=" 2016-04-10 17:31:27 screen. png "alt=" Wkiom1ckhxyj8eqyaaa0cygictu561.png "/>


This article is from "Narcissus" blog, please make sure to keep this source http://10704527.blog.51cto.com/10694527/1762368

Big Data applications for heap sequencing and heap

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.