Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Practice Searching Algorithms
  • MCQs on Searching Algorithms
  • Tutorial on Searching Algorithms
  • Linear Search
  • Binary Search
  • Ternary Search
  • Jump Search
  • Sentinel Linear Search
  • Interpolation Search
  • Exponential Search
  • Fibonacci Search
  • Ubiquitous Binary Search
  • Linear Search Vs Binary Search
  • Interpolation Search Vs Binary Search
  • Binary Search Vs Ternary Search
  • Sentinel Linear Search Vs Linear Search
Open In App
Next Article:
Introduction to Sorting Techniques – Data Structure and Algorithm Tutorials
Next article icon

Introduction to Searching - Data Structure and Algorithm Tutorial

Last Updated : 21 Oct, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Searching is the fundamental process of locating a specific element or item within a collection of data. This collection of data can take various forms, such as arrays, lists, trees, or other structured representations.

Searching-algorithm
Introduction to Searching - Data Structure and Algorithm Tutorial


The primary objective of searching is to determine whether the desired element exists within the data, and if so, to identify its precise location or retrieve it. It plays an important role in various computational tasks and real-world applications, including information retrieval, data analysis, decision-making processes, and more.

Importance of Searching in DSA

  • Efficiency: Efficient searching algorithms improve program performance.
  • Data Retrieval: Quickly find and retrieve specific data from large datasets.
  • Database Systems: Enables fast querying of databases.
  • Problem Solving: Used in a wide range of problem-solving tasks.

Characteristics of Searching

Understanding the characteristics of searching in data structures and algorithms is crucial for designing efficient algorithms and making informed decisions about which searching technique to employ. Here, we explore key aspects and characteristics associated with searching:

1. Target Element:

In searching, there is always a specific target element or item that you want to find within the data collection. This target could be a value, a record, a key, or any other data entity of interest.

2. Search Space:

The search space refers to the entire collection of data within which you are looking for the target element. Depending on the data structure used, the search space may vary in size and organization.

3. Complexity:

Searching can have different levels of complexity depending on the data structure and the algorithm used. The complexity is often measured in terms of time and space requirements.

4. Deterministic vs Non-deterministic:

Some searching algorithms, like binary search, are deterministic, meaning they follow a clear and systematic approach. Others, such as linear search, are non-deterministic, as they may need to examine the entire search space in the worst case.

Applications of Searching:

Searching algorithms have numerous applications across various fields. Here are some common applications:

  • Information Retrieval: Search engines like Google, Bing, and Yahoo use sophisticated searching algorithms to retrieve relevant information from vast amounts of data on the web.
  • Database Systems: Searching is fundamental in database systems for retrieving specific data records based on user queries, improving efficiency in data retrieval.
  • E-commerce: Searching is crucial in e-commerce platforms for users to find products quickly based on their preferences, specifications, or keywords.
  • Networking: In networking, searching algorithms are used for routing packets efficiently through networks, finding optimal paths, and managing network resources.
  • Artificial Intelligence: Searching algorithms play a vital role in AI applications, such as problem-solving, game playing (e.g., chess), and decision-making processes
  • Pattern Recognition: Searching algorithms are used in pattern matching tasks, such as image recognition, speech recognition, and handwriting recognition.

Searching Algorithms:

Searching Algorithms are designed to check for an element or retrieve an element from any data structure where it is stored.

Below are some searching algorithms:

  1. Linear Search
  2. Binary Search
  3. Ternary Search
  4. Jump Search
  5. Interpolation Search
  6. Fibonacci Search
  7. Exponential Search

1. Linear Search:

Linear Search, also known as Sequential Search, is one of the simplest and most straightforward searching algorithms. It works by sequentially examining each element in a collection of data(array or list) until a match is found or the entire collection has been traversed.


Linear-Search
Linear Search

Algorithm of Linear Search:

  • The Algorithm examines each element, one by one, in the collection, treating each element as a potential match for the key you're searching for.
  • If it finds any element that is exactly the same as the key you're looking for, the search is successful, and it returns the index of key.
  • If it goes through all the elements and none of them matches the key, then that means "No match is Found".

Illustration of Linear Search:

Consider the array arr[] = {10, 50, 30, 70, 80, 20, 90, 40} and key = 30

Start from the first element (index 0) and compare key with each element (arr[i]). Comparing key with first element arr[0]. Since not equal, the iterator moves to the next element as a potential match.

Linear-Search-Algorithm-1

Comparing key with next element arr[1]. Since not equal, the iterator moves to the next element as a potential match.

Linear-Search-Algorithm-2

Now when comparing arr[2] with key, the value matches. So the Linear Search Algorithm will yield a successful message and return the index of the element when key is found.

Linear-Search-Algorithm-3

Pseudo Code for Linear Search:

LinearSearch(collection, key):

for each element in collection:

if element is equal to key:

return the index of the element

return "Not found"

Complexity Analysis of Linear Search:

  • Time Complexity:
    • Best Case: In the best case, the key might be present at the first index. So the best case complexity is O(1)
    • Worst Case: In the worst case, the key might be present at the last index i.e., opposite to the end from which the search has started in the list. So the worst-case complexity is O(N) where N is the size of the list.
    • Average Case: O(N)
  • Auxiliary Space: O(1) as except for the variable to iterate through the list, no other variable is used.

When to use Linear Search:

  • When there is small collection of data.
  • When data is unordered.

2. Binary Search:

Binary Search is defined as a searching algorithm used in a sorted array by repeatedly dividing the search interval in half. The idea of binary search is to use the information that the array is sorted and reduce the time complexity to O(log N).

Binary-Seach-1
Binary Search Algorithm

Algorithm of Binary Search:

  • Divide the search space into two halves by finding the middle index “mid”.
  • Compare the middle element of the search space with the key.
  • If the key is found at middle element, the process is terminated.
  • If the key is not found at middle element, choose which half will be used as the next search space.
    • If the key is smaller than the middle element, then the left side is used for next search.
    • If the key is larger than the middle element, then the right side is used for next search.
  • This process is continued until the key is found or the total search space is exhausted.

Illustration of Binary Search:

Consider an array arr[] = {2, 5, 8, 12, 16, 23, 38, 56, 72, 91}, and the target = 23.

  • Calculate the mid and compare the mid element with the key. If the key is less than mid element, move to left and if it is greater than the mid then move search space to the right.
  • Key (i.e., 23) is greater than current mid element (i.e., 16). The search space moves to the right.

Binary-Seach-Algorithm-1

  • Key is less than the current mid 56. The search space moves to the left.

Binary-Seach-Algorithm-2

  • If the key matches the value of the mid element, the element is found and stop search.

Binary-Seach-Algorithm-3

Pseudo Code for Binary Search:

Below is the pseudo code for implementing binary search:

binarySearch(collection, key):

left = 0

right = length(collection) - 1

while left <= right:

mid = (left + right) // 2

if collection[mid] == key:

return mid

elif collection[mid] < key:

left = mid + 1

else:

right = mid - 1

return "Not found"

Complexity Analysis of Binary Search:

  • Time Complexity:
    • Best Case: O(1) - When the key is found at the middle element.
    • Worst Case: O(log N) - When the key is not present, and the search space is continuously halved.
    • Average Case: O(log N)
  • Auxiliary Space: O(1)

When to use Binary Search:

  • When the data collection is monotonic (essential condition) in nature.
  • When efficiency is required, specially in case of large datasets.

3. Ternary Search:

Ternary Search is a searching algorithm that divides the search space into three parts instead of two, as in Binary Search. It is very useful in the case of unimodal functions.

Algorithm Ternary Search:

  • In Ternary Search, start with two midpoints, oneThird and twoThirds, which divide the collection into three roughly equal parts.
  • Compare the elements at oneThird and twoThirds with the target key you're searching for.
  • Three Possibilities:
    • If oneThird contains the key, you're done and return the index of oneThird.
    • If twoThirds contains the key, you're done and return the index of twoThirds.
    • If the key is less than the element at oneThird, eliminate the rightmost one-third of the collection and focus on the left two-thirds.
  • If the key is greater than the element at twoThirds, eliminate the leftmost one-third of the collection and focus on the right two-thirds.
  • Repeat this process iteratively until either key is found or determine that it's not present in the collection.

Example of Ternary Search:

Consider an array arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, and the target = 6.

Ternary-Search
Ternary Search

Complexity Analysis of Ternary Search:

  • Time Complexity:
    • Best Case: O(1)
    • Worst Case: O(log3N)
    • Average Case: O(log3N)
  • Auxiliary Space: O(1)

4. Jump Search:

Jump Search is another searching algorithm that can be used on sorted collections (arrays or lists). The idea is to reduce the number of comparisons by jumping ahead by fixed steps or skipping some elements in place of searching all elements.

Illustration of Jump Search:

Let’s consider the following array: (0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610).

The length of the array is 16. The Jump search will find the value of 55 with the following steps assuming that the block size to be jumped is 4.

  • Jump from index 0 to index 4;
  • Jump from index 4 to index 8;
  • Jump from index 8 to index 12;
  • Since the element at index 12 is greater than 55, we will jump back a step to come to index 8.
  • Perform a linear search from index 8 to get the element 55.

Time Complexity of Jump Search:

  • Time Complexity: O(√n), where "n" is the number of elements in the collection. This makes it more efficient than Linear Search but generally less efficient than Binary Search for large datasets.
  • Auxiliary Space: O(1), as it uses a constant amount of additional space for variables.

Performance Comparison based on Complexity:

linear search < jump search < binary search

5. Interpolation Search

Interpolation Search is an efficient searching algorithm for sorted collections of data, such as arrays or lists. It is an improvement over Binary Search, particularly when the data is uniformly distributed.

6. Fibonacci Search

Fibonacci Search is an efficient searching algorithm used for finding a target value in a sorted collection, such as an array or list. It is similar in principle to Binary Search but uses Fibonacci numbers to determine the positions to be compared.

7. Exponential Search

Exponential Search is a searching algorithm designed to find a target value in a sorted collection, such as an array or list. It combines elements of Binary Search and Linear Search to efficiently locate the target, especially when its position is near the beginning of the collection.

Easy Problems on Searching:

  1. Count 1’s in a sorted binary array
  2. Ceiling in a sorted array
  3. k largest(or smallest) elements in an array
  4. Kth smallest element in a row-wise and column-wise sorted 2D array
  5. Given an array of of size n and a number k, find all elements that appear more than n/k times.

Medium problems on Searching:

  1. Find a peak element
  2. Search an element in a sorted and rotated array
  3. Find the minimum element in a sorted and rotated array
  4. Find the closest pair from two sorted arrays
  5. Allocate Minimum Number of Pages from N books to M students
  6. Assign stalls to K cows to maximize the minimum distance between them

Hard problems on Searching:

  1. Median of two sorted arrays
  2. Median of two sorted arrays of different sizes
  3. Search in an almost sorted array
  4. Find position of an element in a sorted array of infinite numbers
  5. Given a sorted and rotated array, find if there is a pair with a given sum
  6. Longest Increasing Subsequence Size (N log N)



Next Article
Introduction to Sorting Techniques – Data Structure and Algorithm Tutorials

M

mridul_gfg
Improve
Article Tags :
  • Algorithms
  • Searching
  • Data Structures
Practice Tags :
  • Algorithms
  • Data Structures
  • Searching

Similar Reads

  • Introduction to Set – Data Structure and Algorithm Tutorials
    Set Data Structure is a type of data structure which stores a collection of distinct elements. In this article, we will provide a complete guide for Set Data Structure, which will help you to tackle any problem based on Set. Table of Content What is Set Data Structure? Need for Set Data Structure Ty
    15+ min read
  • Introduction to Map – Data Structure and Algorithm Tutorials
    Maps is also known as dictionaries or associative arrays, are fundamental data structures that allow you to efficiently store and retrieve data based on unique keys. This tutorial will cover the basics of maps, including their main ideas, how they are used in different programming languages, and how
    15+ min read
  • Introduction to Sorting Techniques – Data Structure and Algorithm Tutorials
    Sorting refers to rearrangement of a given array or list of elements according to a comparison operator on the elements. The comparison operator is used to decide the new order of elements in the respective data structure. Why Sorting Algorithms are ImportantThe sorting algorithm is important in Com
    3 min read
  • Why Data Structures and Algorithms Are Important to Learn?
    Have you ever wondered why there's so much emphasis on learning data structures and algorithms (DSA) in programming? You might think, "Do I really need to know all this complicated stuff? It doesn't seem useful in real life." Let's dive into why understanding DSA is not just important but essential
    7 min read
  • Commonly Asked Data Structure Interview Questions on Searching
    Searching is a fundamental concept in computer science, involving the process of finding a specific element in a collection of data. Efficient searching techniques are crucial for optimizing performance, especially when dealing with large datasets. Interview questions related to searching often test
    3 min read
  • Introduction to Data Structures
    What is Data Structure?A data structure is a particular way of organising data in a computer so that it can be used effectively. The idea is to reduce the space and time complexities of different tasks. The choice of a good data structure makes it possible to perform a variety of critical operations
    7 min read
  • Top 12 Data Structure Algorithms to Implement in Practical Applications in 2025
    New Year...New Beginning...!!! What's your plan for this year??? (Being a programmer) Of course, if you're a programmer then this year you will also write the code, build the projects and you will solve a lot of coding questions. Let's talk about Data Structures and Algorithms... Heart of computer s
    14 min read
  • Searching and Sorting Algorithm Notes for GATE Exam [2024]
    As you gear up for the GATE Exam 2024, it's time to dive into the world of searching and sorting algorithms. Think of these algorithms as the super-smart tools that help computers find stuff quickly and organize data neatly. These notes are here to guide you through the ins and outs of these algorit
    11 min read
  • Data Structures and Algorithms Online Courses : Free and Paid
    Data Structures and Algorithms are two of the most important skills that every computer science student must have. It is often seen that people with good knowledge of these technologies are better programmers than others and thus crack the interviews of almost every tech giant. Now, you must be thin
    6 min read
  • Why Every Developer Should Learn Data Structures and Algorithms?
    Software developers are regarded as the unknown heroes who design, execute, deploy and manage software programs. It is indeed a lucrative career option that promises insanely high salaries, amazing career growth, and global opportunities. As per the survey software development will witness an amazin
    7 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences