Searching in Data Structure and Its type

Searching in data structures is a fundamental concept in computer science, focusing on finding an element within a data structure. This operation is crucial for numerous applications, such as database management, information retrieval, and the organization of large datasets. The efficiency of a search operation depends on the data structure used and the algorithm implemented.

What is Searching?

Searching is a process of finding any given data in a large collection of data. In memory, data is organized in an array, linked list, graph, and tree, etc.

If required data is available in a file or any data structure, we will get a related output otherwise to get no item found or any customizable message. We can perform a searching operation in both primary and secondary memory. The complexity of the searching algorithm depends on the number of comparisons required to find specific data from the data collection.

Types of Searching Technique

  1. Sequential Search/Linear Search
  2. Binary Search
  3. Jump Search
  4. Interpolation Search
  5. Exponential Search
  6. Fibonacci Search
  7. Hashing
  8. Depth-First Search (DFS)
  9. Breadth-First Search (BFS)

A method that checks each element in a list one by one until it finds the target element or reaches the end of the list.

Imagine looking for your shoe in a line of shoes. You check each one until you find yours. It’s simple and works even if the shoes are all mixed up.

  • Example: Finding a specific toy in a single-layer toy box.
  • Uses: Best when the list is small or unsorted.
  • Complexity:
    • Best Case: O(1) – The element is found at the first position.
    • Worst Case: O(n) – The element is at the last position or not present at all.
  • Real-time Application: Searching for a contact in a short list on a phone.

A technique used on sorted lists that divides the search interval in half repeatedly to locate the target element more quickly than linear search.

Think about finding a word in a sorted list. You open it in the middle, decide if your word is before or after, and keep halving the section until you find it.

  • Example: Looking up a word in a dictionary by halving the search area each time.
  • Uses: Efficient for sorted lists.
  • Complexity:
    • Best Case: O(1) – The element is at the middle of the list.
    • Worst Case: O(log n) – The search may require looking through the entire tree depth.
  • Real-time Application: Finding a player’s score in a sorted leaderboard.

A search strategy that works on sorted lists by jumping ahead by fixed steps and then performing a linear search within the identified block.

It’s like skipping stones across a pond while looking for a special one. You jump ahead several stones at once, then look around carefully once you’ve gone too far.

  • Example: Skipping through chapters in a textbook to find a topic.
  • Uses: Useful in sorted arrays where binary search is too complex.
  • Complexity:
    • Best Case: O(1) – The element is close to the first position.
    • Worst Case: O(√n) – The element is towards the end of the previous block or not present.
  • Real-time Application: Searching in database indexes that are too large for memory.

An algorithm that assumes that the elements are uniformly distributed in a sorted list and calculates where in the list the search key is likely to be for faster finding.

Suppose you’re guessing the price of a prize on a game show. You make a guess based on the prices you know, getting closer each time until you guess right.

  • Example: Picking a specific level in a video game based on progress.
  • Uses: Great for uniformly distributed, sorted data.
  • Complexity:
    • Best Case: O(1) – When the element being searched is the initial guess.
    • Worst Case: O(n) – When elements are not uniformly distributed, the algorithm may degrade to linear search.
  • Real-time Application: Lookup in phone books or address directories.

A method that first determines the range where the search key might be by doubling the bounds and then applies binary search within this range.

Imagine zooming out on a map to see more, then zooming in once you spot your destination. First, you find the right area quickly, then you focus in for details.

  • Example: Expanding a search for a Wi-Fi signal by doubling the distance.
  • Uses: Effective for unbounded or infinite sorted lists.
  • Complexity:
    • Best Case: O(1) – The element is at the first position.
    • Worst Case: O(log n) – Involves first finding the range using exponential bounds and then binary searching that range.
  • Real-time Application: Searching in unbounded streaming data.

A search technique that uses Fibonacci numbers to divide the list into sections and finds the target by eliminating sections where the element cannot be.

You’re dividing a treasure map into sections that get smaller like a Fibonacci sequence. You check the bigger sections first, narrowing down where the treasure is hidden.

  • Example: Organizing books by Fibonacci sequence sizes and searching accordingly.
  • Uses: Useful for sorted data; reduces the number of comparisons.
  • Complexity:
    • Best Case: O(1) – The element is found during the first comparison.
    • Worst Case: O(log n) – Similar to binary search, but without the need for predefined intervals.
  • Real-time Application: Accessing data in databases where the split of data matters.

7). Hashing

A search mechanism that uses a hash function to compute an index into an array of buckets or slots, from which the desired value can be found.

Think of storing your toys in boxes with labels. When you need a specific toy, you just look at the label and go straight to it. Super fast and easy.

  • Example: Finding a word’s meaning quickly using a dictionary app.
  • Uses: Fast retrieval from a large dataset.
  • Complexity:
    • Best Case: O(1) – Direct access to the element through its hash, without any collisions.
    • Worst Case: O(n) – All elements collide into the same hash bucket, turning the hash table search into a linear search.
  • Real-time Application: Looking up a user’s profile in a social network.

8). Depth-First Search (DFS)

A graph traversal method that explores as far as possible along each branch before backtracking, used for searching all nodes in a graph or tree structure.

Exploring a cave, you follow one path as far as you can before turning back and trying another. You see every part of the cave, one path at a time.

  • Example: Exploring every path in a maze before trying a new direction.
  • Uses: Solving puzzles, pathfinding.
  • Complexity:
    • Best Case: O(1) – The target node is the first visited.
    • Worst Case: O(V + E) for graphs, where V is the number of vertices and E is the number of edges. It may need to explore all nodes and edges.
  • Real-time Application: Web crawlers scanning every link on a webpage deeply.

9). Breadth-First Search (BFS)

A technique for traversing or searching tree or graph data structures that starts at the root and explores all neighbors at the present depth prior to moving on to nodes at the next depth level.

You’re at a party, saying hi to everyone at your table before moving to the next. You meet everyone, making sure not to skip anyone close by.

  • Example: Spreading out a search evenly across all directions in a maze.
  • Uses: Finding the shortest path, level-wise traversal.
  • Complexity:
    • Best Case: O(1) – The target node is the first visited.
    • Worst Case: O(V + E) – Similar to DFS, BFS might need to explore all vertices and edges in the worst case.
  • Real-time Application: GPS navigation systems finding the shortest route.

Each search method serves a unique purpose, optimized for specific data structures and use cases to efficiently locate elements within collections.