1 million number of first 100

Source: Internet
Author: User

In the case of limited memory, the first 100 of the 1 million numbers are calculated.

Problem-solving ideas: The first thought is to divide 1 million numbers into 100 parts, one is 10,000, and then 10,000 to build a minimum heap to find the first 100, 100 is 10,000 so you can find the first 100;

The code is as follows:


#include <windows.h>

#include <vector>

#include <ctime>

#include <cstdlib>

#include <iostream>

using namespace Std;


const int n=10000;

const int k=100;


void Createarray (Vector<int>&array)

{

Srand (Time (0));

Array.reserve (N);

for (size_t i = 0; i < N; i++)

{

Array.push_back (rand ()% 10000);

}

for (size_t j = n-k; J < N; J + +)

{

ARRAY[J] = rand ()%N;

}

}



void Adjustdown (int* A, size_t size, int root)

{

int child = root * 2 + 1;

while (Child < size)

{

if (child + 1 <size && A[child + 1] < A[child])

{

++child;

}

if (A[child] < A[root])

{

Swap (A[child], a[root]);

root = child;

Child = 2 * root + 1;

}

Else

{

Break

}

}

}


void Gettop (Vector<int>&array)

{

int A[k] = {};

for (size_t i = 0; i < K; i++)

{

A[i] = Array[i];

}

for (int i = (K-2)/2; I >= 0; i--)

{

Adjustdown (A, K, i);

}

for (int j = K; J < N; J + +)

{

if (A[0]<array[j])

{

A[0] = Array[j];

Adjustdown (A, K, 0);

}

}

for (size_t i = 0; i < K; i++)

{

cout << A[i] << "";

}

cout << Endl;

}

void Test ()

{

vector<int>array;

Createarray (array);

Gettop (array);


}


This article is from the "Stream Wind" blog, please be sure to keep this source http://xiexiankun.blog.51cto.com/10785425/1775367

1 million number of first 100

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.