Javascript: I need a good data structure to keep a sorted list

喜你入骨 提交于 2019-12-22 08:45:23

问题


This would probably be implemented as a tree or something? My point is it needs to be efficient.

I don't know where to find good implementations of data structures for Javascript for something like this, though. I don't want to have to roll my own if I can avoid it.

Help appreciated.


回答1:


Depends why you need it. For instance, if you only need the top element, this binary heap might be okay for you. Otherwise, implement a binarySearch and insertSorted functions for arrays, should not be more than ten-fifteen lines. Unless you plan on having thousands upon thousands of elements; then it makes more sense to just insert in bulk and then sort using the builtin.




回答2:


How about a simple array, sorted after each update?



来源:https://stackoverflow.com/questions/3809800/javascript-i-need-a-good-data-structure-to-keep-a-sorted-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!