1. Heap algorithm Top, Time complexity O(LogN)

```function top(arr,comp){
if(arr.length == 0){return ;}
var i = arr.length / 2 | 0 ;
for(;i >= 0; i--){
if(comp(arr[i], arr[i * 2])){exch(arr, i, i*2);}
if(comp(arr[i], arr[i * 2 + 1])) {exch(arr, i, i*2 + 1);}
}
return arr;
}
function exch(arr,i,j){
var t = arr[i];
arr[i] = arr[j];
arr[j] = t;
}
```

2. call K Implementation of secondary heap , The time complexity is O(K * LogN)

```function topK(arr,n,comp){
if(!arr || arr.length == 0 || n <=0 || n > arr.length){
return -1;
}
var ret = new Array();
for(var i = 0;i < n; i++){
var max = top(arr,comp);
ret.push(max);
arr.splice(0,1);
}
return ret;
}```

3. Testing

```var ret = topK(new Array(16,22,91,0,51,44,23),3,function (a,b){return a < b;});
console.log(ret);```

## Using heap to implement Top K Algorithm JS More articles on Implementation

1. Programmer programming art ： The third chapter continued 、Top K The realization of algorithm problem

Programmer programming art : The third chapter continued .Top K The realization of algorithm problem author :July,zhouzhenren,yansha.     thank : Microsoft 100 Question implementation group , Rhapsody group .     Time :2011 year 05 month 08 Japan    ...

2. Priority queue PriorityQueue Realization Big and small root pile solve top k problem

Reprint :https://www.cnblogs.com/lifegoesonitself/p/3391741.html PriorityQueue It's from JDK1.5 Start to provide new data structure interface , It is based on ...

3. Priority queue implementation Big and small root pile solve top k problem

From :http://my.oschina.net/leejun2005/blog/135085 Catalog :[ - ] 1. know  PriorityQueue 2. application : seek Top K Big / Small The elements of 3 ...

4. hihoCoder 1133 Two points &#183; Two of them k decimal （TOP K Algorithm ）

#1133 : Two points · Two of them k decimal The time limit :10000ms Single point time limit :1000ms Memory limit :256MB describe In the last time we knew Nettle Play with < Warship これ>,Nettle The garrison house of is very ...

5. Top K Algorithm

Application scenarios : The search engine will record all the search strings used by the user every time through the log file , The length of each query string is 1-255 byte .        Let's say there are 10 million records ( These query strings have high repeatability , Although the total is 1 Ten million , But if ...

6. Top K Algorithm details

http://xingyunbaijunwei.blog.163.com/blog/static/7653806720111149318357/ Problem description          Baidu interview questions :        ...

7. top k Algorithm

For an unordered array A[p..r], Find the second in the array k Small elements . How to consider Sort ( Partial sorting ) Not to mention ..o(nlgn), Of course, if you want to take the value all the time in the actual situation , After sorting, of course , One time , Later on O(1) We're talking about ...

8. existing n A random ordinal number , Is greater than 1000 , Let's take the top ten , The time complexity is o(n), top10, perhaps topK, List of application scenarios Top：10, Heap implementation Top k

One .topK python Realization   def topk(k, lst): top = [0 for i in range(k)] # Generate a length of K Ordered list of for item in lst: # loop ...

9. Baidu interview questions &mdash;&mdash;top K Algorithm

demand From 100 million data , Find out the smallest 10 Number . analysis The most stupid way is to put 100 million data , Sort from small to large , Then take the front. 10 individual . In this case , Even if the use time complexity is nlogn Fast or heap arrangement of , Because elements move frequently , efficiency ...

## Random recommendation

1. [ Reprint ]Average Manager vs. Great Manager Explained in 10 sketches

Assigning Tasks     Delivering News     Conducting 1:1s     Giving Feedback     Dealing with Turbule ...

2. form Form submission controller The value received by layer is garbled

Today I met a problem of Chinese garbled code , The general situation is like this : There's one at the front desk form Forms , Some of them input Control for , The value is in Chinese ,form The form only sets id='form1' method='post'  action='xx ...

3. win8 with jdk

Win8 To configure jdk 1.7 environment variable    Environmental Science :win8(32 position )64 It's about the same        jdk1.7    1. Right click on the computer - attribute - Advanced system setup - senior - environment variable , eject “ environment variable ” Dialog box , The main thing is to change the ring below ...

4. C# Commonly used encryption processing

AES using System; using System.Security.Cryptography; using System.Text; namespace Common { public c ...

5. One Oracle Examples of triggers

CREATE OR REPLACE TRIGGER WoStateChange AFTER UPDATE on csdbuser.T_PD_WorkOrder for each row declare ...

6. Python Basics 【 Chapter 9 】 operation

Python The operator of is more popular and almost no different from other languages One . Arithmetic operator " + " Addition operation >>> 1+3 4 The addition operation extends " Enhanced assignment operation " ...

7. Deep learning tensorflow （ One ）

One .TensorFlow brief introduction 1.TensorFlow Definition : tensor  : tensor ,N Dimension group Flow   :  flow , Computation based on data flow graph TensorFlow : The calculation of tensor flowing from one end of the image to the other ...

8. Luogu P2572 [SCOI2010] Sequence of operations (ODT)

Answer key The question Topic link Sol ODT Board question ..... // luogu-judger-enable-o2 #include<bits/stdc++.h> #define LL long l ...

9. Alibaba cloud ECS build FTP The server

One . Open it before you start 21 Port permissions ; Two . add to IIS role ; 3、 ... and . add to ftp user ; Four . Steps are as follows : 5、 ... and . Use add in user login ftp;

10. Java Study notes 13 :Java Classes and objects in

Java Classes and objects in One : What is an object : In general " Everything is object ", Everything that exists objectively is the object . It's the specific information that computers focus on . object (object) It's one thing . An object . A noun , Or you can get ...