ArrayList BinarySearch vs Contains [英] ArrayList BinarySearch vs Contains

查看:94
本文介绍了ArrayList BinarySearch vs Contains的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

哪个更适合与ArrayList一起使用:BinarySearch或Contains?


列表中只有字符串,它将被排序。


谢谢,


Tom

Which is better to use with an ArrayList: BinarySearch or Contains?

The list is only going to have strings in it and it will be sorted.

Thanks,

Tom

推荐答案

" tshad" < ts ********** @ ftsolutions.com写信息

news:u8 ************** @ TK2MSFTNGP04.phx.gbl ...
"tshad" <ts**********@ftsolutions.comwrote in message
news:u8**************@TK2MSFTNGP04.phx.gbl...

哪个更适合与ArrayList一起使用:BinarySearch或Contains?


该列表仅用于有字符串,它将被排序。
Which is better to use with an ArrayList: BinarySearch or Contains?

The list is only going to have strings in it and it will be sorted.



然后BinarySearch更好。包含将是O(n)操作总是

(因为它不知道数据是否被排序),而BinarySearch是一个

O(lg(n) ))操作。只要确保数据真的排序好了!


-cd

Then BinarySearch is better. Contains will be an O(n) operation always
(since it doesn''t know whether the data is sorted), while BinarySearch is an
O(lg(n)) operation. Just make sure that the data really is sorted!

-cd


" tshad" < ts ********** @ ftsolutions.com写信息

news:u8 ************** @ TK2MSFTNGP04.phx.gbl ...
"tshad" <ts**********@ftsolutions.comwrote in message
news:u8**************@TK2MSFTNGP04.phx.gbl...

哪个更适合与ArrayList一起使用:BinarySearch或Contains?


该列表仅用于有字符串,它将被排序。
Which is better to use with an ArrayList: BinarySearch or Contains?

The list is only going to have strings in it and it will be sorted.



包含O(n)

BinarySearch是O(log(n))


对于小型集合包含可能更快

对于大型集合BinarySearch更快


如果您经常在集合前面寻找数据

包含可能有优势


简而言之,这取决于物品数量和正常使用情况。


希望这个帮助

Bill

Contains is O(n)
BinarySearch is O(log(n))

For small collections Contains is probably faster
For large collections BinarySearch is Faster

If you are frequently looking for data near the front of the collection
Contains might have an edge

In short, it depends on the number of items and normal usage.

Hope this helps
Bill


" Carl Daniel [VC ++ MVP]" < cp ***************************** @ mvps.org.nospam>

写道消息新闻:O9 ************** @ TK2MSFTNGP03.phx.gbl ...
"Carl Daniel [VC++ MVP]" <cp*****************************@mvps.org.nospam >
wrote in message news:O9**************@TK2MSFTNGP03.phx.gbl...

" tshad" < ts ********** @ ftsolutions.com写信息

news:u8 ************** @ TK2MSFTNGP04.phx.gbl ...
"tshad" <ts**********@ftsolutions.comwrote in message
news:u8**************@TK2MSFTNGP04.phx.gbl...

>哪个更适合与ArrayList一起使用:BinarySearch或Contains?

该列表只有字符串在它,它将被排序。
>Which is better to use with an ArrayList: BinarySearch or Contains?

The list is only going to have strings in it and it will be sorted.



然后BinarySearch更好。包含将是O(n)操作总是

(因为它不知道数据是否被排序),而BinarySearch是

和O(lg(n) ))操作。只需确保数据真正排序!


Then BinarySearch is better. Contains will be an O(n) operation always
(since it doesn''t know whether the data is sorted), while BinarySearch is
an O(lg(n)) operation. Just make sure that the data really is sorted!



O(n)和O(lg(n))是什么意思?


谢谢,


Tom

What does O(n) and O(lg(n)) mean?

Thanks,

Tom


这篇关于ArrayList BinarySearch vs Contains的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆