Binary Search

Binary Search uses Divide and Conquer algorithm to search given element in a sorted array.

Procedure:

  1. Divide array into half
  2. Compare given value with middle element, if matches return index
  3. If the value is greater then the middle element then values is in the left array then repeat the process with the left array.
  4. 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

Advertisements

LinkedList

LinkedList is a collection class which extends AbstractList and implements List interface.

  1. LinkedList underlying data structure is doubly Linkedlist.
  2. It doesn’t implements Random access interface so we can’t access the elements randomly.
  3. It is not synchronized. If we want to use LinkedList with multi threading then we need to synchronized explicitly using  Collections.synchronizedList(List l).
  4. 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.
  5. Adding and removing elements is lightweight process in LinkedList, because it just modify the address of a node.
  6. LinkedList requires more memory than ArrayList because each node holds previous element address, data and next element address.