java - searching an unorder list without converting it to an array -
Is there a way to search the object in a linked list of objects in a way? I thought that you just have a sort of sorting and a binary search? Thanks
This is not a good way, IMO if you type Collections.sort (list) < / Code>, where a
list
is a linkededlist
, it templates a temporary array that copies it to list
, And then copy it to the O (NlogN)
plus 2 * O (N)
copies, but when you type a binary search (like Collections.binarySearch (list)
, try copying Every search is done by O (N)
list traversal operation. Well sorting list is not bothered!
Another way to convert an array into an array or an ArrayList , And then search the sort and array / ArrayList. It gives a copy to set up in a way, and o (login)
for every search.
But none of these is the best way. It depends on how often you search Connection can.
-
If you just want to do a search on the list, then
list.contains (...)
toO (N)
Calling. ..and that's better than sorting and binary search. -
If you want to search for a list at any time, you probably create a list of entries in HashSet. The HashSet build is
O (N)
and the search isO (1) . (It assumes that you do not need your balance sheet.)
If you want to do multiple searches on that list, the order changes, where the order is not important, Change the list will be
O (1)
andO (1)
for each addition / removal to increase the cost of updating HSSAT for every search with a hashet.If you want to do multiple searches on that list that keeps changing and the order is important, replace the listing with the entry-indexed LinkedHashMap. For each search, it will be
O (1)
andO (1)
to add / delete it ... but with a large constant proportionality compared to a hashet.
Comments
Post a Comment