Scala writing algorithms-solving topk with small Gan

Source: Internet
Author: User

The TOPK problem is to get the largest (or smallest) number of k from a large amount of data, such as looking for the top 500 students from the entire school.
This problem can be solved by small Gan. The idea is to first put the first k number in the source data into the heap, and then build the heap so that it remains in the heap (as simple as the K-insert operation). Then, from the source data, each element after the K data is compared to the root node of the heap (the small root heap is the smallest If it is less than root, then direct pass; if it is greater than, then execute headp.deleteMin , then insert the element into the heap and keep the heap again. It is necessary to maintain the sequence of the upper and lower filtration processes.
Examples are:

ObjectMainextendsapp{ValArray=array (0, -, -, +,3,3768,345,3,343,545,455,7567,657, the,65756,756,756,756,7657,657,657,4,534,535,345343,423,4,46546,546,544,546,546,345,345,435,34534, -,5345, $, $,435, +, Wu, *,435,435, the,5,435, $, $, $,6576,7, $, About,7, $, +,543, -,453, $,345, the)//array starting from 1  Valk=3  Valheap=NewHeap1to Kforeach(I=>heap.Insert(Array(i))) K+1to array.length-1 foreach(i=>{ while(Heap.GetSize>Ten) {heap.Deletemin}if(Array(i) >heap.Getmin) {heap.DeleteminHeap.Insert(Array(i))}}) heap.Print}

And again, Heap how it's achieved.
The use of Scala often comes across the option of changing and not changing variables, which is a problem.
There for is also the derivation formula, which is actually map flatMap equal to the grammatical sugar.

Code:

classHeap {typeT=intPrivate varSize=0  Valelemnts=NewArray[t] ( $)//Put the position of the index=0 at 2n 2n+1 N/2  defGetsize:int=returnSizedef Insert(x:t): unit={def Loop(i:int): int={if(elemnts(i/2) <=x)returnIelemnts(i) =elemnts(i/2)Loop(i/2)    }ValI=Loop(Size+1)elemnts(i) =x size+=1}defdeletemin:t={def Loop(i:int): int={if(i>size)returni/2      Valleft=elemnts(2*i)Valright=elemnts(2*i+1)if(Left<right) {elemnts(i) =leftLoop(i*2)      }Else{elemnts(i) =rightLoop(2*i+1)      }    }Valresult=elemnts(1)Vallast=elemnts(size)ValI=Loop(1)elemnts(i) =last size-=1    returnResult}defprint:unit=1To sizeforeach(i=>println(elemnts(i)))defgetmin:t=return elemnts(1)//Code Pro Test Error}

At the time of implementation, insert deleteMin because Scala does not have break a keyword (although you can use Breakable this class implementation, actually by throwing an exception to simulate break, not flexible), for the implementation of consideration (insert), consider recursion to simulate a for loop.
Code:

  definsert(x:T):Unit={    defloop(i:Int):Int={      if(elemnts(i/2)<=x)        return//如果父亲节点比待插入值x小,则本节点应该插入x      elemnts(i)=elemnts(i/2//上虑      loop(i/2)    }    val i=loop(size+1//返回待x插入的位置    elemnts(i)=x    size+=1  }

Scala-based code is easy to remember and fairly robust compared to the C language version:

void insert(Element x,Heap* h){    int i=0;    if(isFull(h))        error("full");    for(i=++h->size;h->elements[i/2]>x;i/=2)        h->elements[i]=h->elements[i/2];    h->elements[i]=x;}

Scala writing algorithms-solving topk with small Gan

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.