Is there any use of Binary Search Algorithm in Sorted Linked List? Or is it useful any how in any way?
I know that in array it is very efficient to use Binary Search as we know the address of all elements. But in linked List, we have to traverse through every Node.
The reason to use binary search is to find a number in o(log n). It is however impossible for a linked list. The use of a tree or an array is suggested. Take a look at this:
how to apply binary search O(log n) on a sorted linked list?
In Linked List, binary search may not achieve a complexity of O(log n) as described by (#Good Luck) but least can be achieved a little by using Double Pointer Method (provided the Linked list is in Sorted order) as described here in this research work:
http://www.ijcsit.com/docs/Volume%205/vol5issue02/ijcsit20140502215.pdf
Related
Can anyone explain the difference between binary search and binary search tree with example?Are they the same?Reading the internet it seems the second is only for trees and binary search does not follow this rule.And how to check the presence of a number in O(log(n)) time?
Binary search is an algorithm used on straightforward sorted arrays, which runs in O(log n).
Updating a sorted array is O(n).
A binary tree is a data structure that has both O(log n) search and update (Ignoring problems of balancing).
An interesting comparison is at the end of this chapter.
I'm wondering, is there any difference between performance of those, provided binary search is used for sorted linked list insertion, search. And in which situations they perform differently or maybe for which purposes, say, list will be unusable or vice versa.
You can't do a binary search on a linked list (single or double) simply because there's no way to get to the middle of the list without traversing half of it (from one end).
There's no doubt a form of multi-level skip list that will do that but it seems to me that's just emulating a binary tree with a more complex structure.
A sorted linked list tends to be O(n) for search, insertion and deletion (the actual insertion /deletion itself is O(1) but you still have to find the insertion or deletion point first).
Alternatively, binary trees (balanced ones) are O(log n) for search, insertion and deletion (all these operations are proportional to the height of the tree).
What is the cost of finding an element in a linked list? I know that the cost of finding an element in a balanced binary search tree which is O(log n), but what about a linked list?
If you know nothing about the elements in the linked list and have no pointers into the list, the cost of searching for an element in a linked list is O(1) in the best case and O(n) in the worst-case. In the best case, you find the element at the very front, and in the worst case have to look at all elements before deciding that the element you're searching for isn't there.
This is much slower than a balanced binary search tree in the worst-case, so there are some variations on the linked list designed to speed up access. The skip list, for example, uses multiple parallel linked lists to make it possible to "skip" over elements in the list. This requires the elements to be stored in sorted order, but it does decrease the lookup time to expected O(log n).
Hope this helps!
I have a problem choosing the right data structure/s, these are the requirements:
I must be able to insert and delete elements
I must also be able to get the index of the element in the collection (order in the collection)
Elements has an unique identifier number
I can sort (if necessary) the elements using any criterium
Ordering is not really a must, the important thing is getting the index of the element, no matters how is internally implemented, but anyway I think that the best approach is ordering.
The index of the element is the order inside the collection. So some kind of order has to be used. When I delete an element, the other elements from that to the end change their order/index.
First approach is using a linked list, but I don't want O(n).
I have also thinked about using and ordered dictionary, that would give O(log n) for lookup/insert/delete, isn't it?
Is there a better approach? I know a TRIE would give O(1) for common operations, but I don't see how to get the index of an element, I would have to iterate over the trie and would give O(n), am I wrong?
Sounds like you want an ordered data structure, i.e. a (balanced) BST. Insertion and deletion would indeed be O(lg n), which suffices for many applications. If you also want elements to have an index in the structure, then you'd want an order statistic tree (see e.g., CLR, Introduction to Algorithms, chapter 14) which provides this operation in O(lg n). Dynamically re-sorting the entire collection would be O(n lg n).
If by "order in the collection" you mean any random order is good enough, then just use a dynamic array (vector): amortized O(1) append and delete, O(n lg n) in-place sort, but O(n) lookup until you do the sort, after which lookup becomes O(lg n) with binary search. Deletion would be O(n) if the data is to remain sorted, though.
If your data is string-like, you might be able to extend a trie in the same that a BST is extended to become an order statistic tree.
You don't mention an array/vector here, but it meets most of these criteria.
(Note that "Elements has a unique identifer number" is really irrespective of datastructure; does this mean the same thing as the index? Or is it an immutable key, which is more a function of the data you're putting into the structure...)
There are going to be timing tradeoffs in any scenario: you say linked list is O(n), but O(n) for what? You don't really get into your performance requirements for additions vs. deletes vs. searches; which is more important?
Well if your collection is sorted, you don't need O(n) to find elements. It's possible to use binary search for example to determine index of element. Also it's possible to write simple wrapper about Entry inside your array, which remember its index inside collection.
Recently I came across one interesting question on linked list. Sorted singly linked list is given and we have to search one element from this list.
Time complexity should not be more than O(log n). This seems that we need to apply binary search on this linked list. How? As linked list does not provide random access if we try to apply binary search algorithm it will reach O(n) as we need to find length of the list and go to the middle.
Any ideas?
It is certainly not possible with a plain singly-linked list.
Sketch proof: to examine the last node of a singly-linked list, we must perform n-1 operations of following a "next" pointer [proof by induction on the fact that there is only one reference to the k+1th node, and it is in the kth node, and it takes a operation to follow it]. For certain inputs, it is necessary to examine the last node (specifically, if the searched-for element is equal to or greater than its value). Hence for certain inputs, time required is proportional to n.
You either need more time, or a different data structure.
Note that you can do it in O(log n) comparisons with a binary search. It'll just take more time than that, so this fact is only of interest if comparisons are very much more expensive than list traversal.
You need to use skip list. This is not possible with a normal linked list (and I really want to learn if this is possible with normal list).
In Linked List, binary search may not achieve a complexity of O(log n) but least can be achieved a little by using Double Pointer Method as described here in this research work: http://www.ijcsit.com/docs/Volume%205/vol5issue02/ijcsit20140502215.pdf
As noted, this is not in general possible. However, in a language like C, if the list nodes are contiguously allocated, it would be possible to treat the structure as an array of nodes.
Obviously, this is only an answer to a trick question variant of this problem, but the problem is always an impossibility or a trick question.
Yes, it is possible in java language as below..
Collections.<T>binarySearch(List<T> list, T key)
for binary search on any List. It works on ArrayList and on LinkedList and on any other List.
Use MAPS to create LINK LISTS.
Map M , M[first element]=second element , M[second element]=third element ,
...
...
its a linked list...
but because its a map...
which internally uses binary search to search any element..
any searching of elements will take O(log n)