View on GitHub

Notes

reference notes

Searching

What is Searching?

Searching in data structures refers to the process of finding required information from a collection of items stored as elements in the computer memory. These items can be in different forms, such as an array, linked list, graph, or tree.

Example Applications:

Type of Searching

  1. Sequential Search:
    • The list or array is traversed sequentially, checking every component of the set.
    • Example: Linear Search.
  2. Interval Search:
    • Specifically designed for searching in sorted data structures.
    • More efficient than Linear Search, targeting the center of the search structure and dividing the search space in half.
    • Example: Binary Search.
  3. Search by Hashing

Algorithm:

Time Complexity:

Examples:

Advantages:

Disadvantages:

Binary Search is a searching algorithm used in a sorted array by repeatedly dividing the search interval in half. It works on the principle of divide and conquer.

Algorithm:

  1. Mark the first element as LOW and last element as HI.
  2. Find the mid-point: MID = (LOW + HI) / 2.
  3. If key = MID, return index.
  4. If key < MID, set HI = MID – 1.
  5. If key > MID, set LOW = MID + 1.
  6. Repeat until the value is found or LOW and HI cross each other.

Runtime Complexity:

Examples:

What is Hashing?

Example:

ID   | Name               | Course
-----|----------------------|-------
10050| Ali Bin Abu         | BCS(SE)
10048| Lim Li Hiang        | BCS(SN)
10021| Ramu A/L Ravi        | BCS(CS)
10079| Sameer               | BIT(GM)
10087| John Lee             | BIT(IS)
10033| Hussein              | BIT(VM)
...  | ...                  | ...

Requirements of Hashing Function

Types of Hashing Function

1. Truncation Method

Example:

2. Digit Extraction Method

Example:

3. Modular Arithmetic

Example:

Collision

Linear Probing

Example:

Quadratic Probing

Example:

Chaining

Advantages:

  1. Simple to implement.
  2. Hash table never fills up, and elements can be added indefinitely.
  3. Less sensitive to hash function or load factors.
  4. Suitable when it’s unknown how many or how frequently keys may be inserted or deleted.
  5. Deletion process is easier.

Disadvantages:

  1. Cache performance is not optimal as keys are stored in a linked list.
  2. Wastage of space as some parts of the hash table are never used.
  3. Search time can become O(n) in the worst case if the chain becomes long.
  4. Uses extra space for links.

Example:

Double Hashing

Example: