Introduction to Searching
☰Fullscreen
Table of Content:
Searching is the process of finding a specific element in a collection of data.
In Java, arrays are one of the most commonly used data structures, and very often we need to search an element inside an array.
Why Searching Is Needed?
-
To find a student’s roll number
-
To check if a value exists in a list
-
To retrieve data quickly
-
To validate user input
Example
If an array contains marks:{45, 67, 89, 23, 90}
Searching means checking whether a value like 89 is present or not.
Searching is a basic operation and forms the foundation of many advanced algorithms.
Types of Searching Techniques
Searching techniques are methods used to locate an element in an array.
Main Types of Searching:
-
Linear Search
-
Binary Search
Classification Based on Data:
| Array Type | Searching Method |
|---|---|
| Unsorted Array | Linear Search |
| Sorted Array | Binary Search |
Each technique has its own rules, speed, and use cases.
- Assignment 1: Write a Java program to search a user-entered value in an array.
- Assignment 2: Write a Java program to count how many times a value appears in an array.
- Assignment 3: Write a Java program to search multiple values in an array.
- Assignment 4: Write a Java program to find the position (index) of the largest element in an array.