Binary Search uses Divide and Conquer algorithm to search given element in a sorted array.
- Divide array into half
- Compare given value with middle element, if matches return index
- If the value is greater then the middle element then values is in the left array then repeat the process with the left array.
- If the value is less than the middle element then the value is in the right array then repeat the process with the right array.
Time complexity for binary search is O(logn).
Example : https://github.com/santhoshgudla/DSTraining
LinkedList is a collection class which extends AbstractList and implements List interface.
- LinkedList underlying data structure is doubly Linkedlist.
- It doesn’t implements Random access interface so we can’t access the elements randomly.
- It is not synchronized. If we want to use LinkedList with multi threading then we need to synchronized explicitly using Collections.synchronizedList(List l).
- Accessing elements with LinkedList is heavy process because if we want to access a element it needs to traverse full list if the element in the last position.
- Adding and removing elements is lightweight process in LinkedList, because it just modify the address of a node.
- LinkedList requires more memory than ArrayList because each node holds previous element address, data and next element address.