Linear Search in a 2D array using Java
Searching for an element in a two-dimensional (2D) array adds an extra layer of complexity to the classic Linear Search algorithm. In this blog post, we’ll explore the step-by-step implementation of Linear Search in a 2D array using Java, unravelling the intricacies of this fundamental algorithm when applied to multi-dimensional data structures.
Understanding 2D Array Linear Search:
A 2D array introduces rows and columns, and searching for an element involves traversing through both dimensions to find the target value. Searching linearly in a 2D array entails sequentially checking each element until finding the desired element or exploring the entire array.
Java Implementation:
Consider the following Java program implementing Linear Search in a 2D array:
public class LinearSearch2D {
// Method to perform Linear Search in a 2D array
public static int[] linearSearch2D(int[][] array, int target) {
// Loop through rows
for (int i = 0; i < array.length; i++) {
// Loop through columns in each row
for (int j = 0; j < array[i].length; j++) {
if (array[i][j] == target) {
// Return indices if the target element is found
return new int[]{i, j};
}
}
}
// Return {-1, -1} if the target element is not found in the 2D array
return new int[]{-1, -1};
}
// Main method for testing the Linear Search in 2D array
public static void main(String[] args) {
// Example 2D array
int[][] array2D = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
int targetElement = 6; // Target element for demonstration
// Perform Linear Search and store the result in the 'result' array
int[] result = linearSearch2D(array2D, targetElement);
// Check if the target element was found and print the result accordingly
if (result[0] != -1) {
System.out.println("Element " + targetElement + " found at position: (" + result[0] + ", " + result[1] + ")");
} else {
System.out.println("Element " + targetElement + " not found in the 2D array");
}
}
}
Breaking Down the Implementation:
Let’s break down the Java program that implements Linear Search in a 2D array:
1. linearSearch2D Method:
- This method takes a 2D array (
array) and a target element (target) as parameters. - It uses nested loops to traverse through each element in the 2D array.
- If the target element is found, it returns an array containing the indices of the element (
new int[]{i, j}). - In case the algorithm doesn’t find the target element, it returns {-1, -1}.
2. Main Method:
- In the
mainmethod, a 2D array (array2D) is declared and initialized. - The target element (
targetElement) is set to 6 for demonstration purposes. - The
linearSearch2Dmethod is called with the 2D array and the target element, and the result is stored in theresultarray. - The program then checks if the first index of the result is not -1. If the condition is true, the program prints the position where it found the element. If false, it prints a message indicating the element’s absence in the 2D array.
3. Execution:
- In this example, the 2D array reveals that it found the target element 6 at position (1, 2).
- The program prints: “Element 6 found at position: (1, 2)”.
This program demonstrates how to adapt Linear Search for use in a 2D array, revealing the indices of the target element upon discovery.
Output:
Element 6 found at position: (1, 2)Key Takeaways:
- Linear Search in a 2D array involves nested loops to traverse rows and columns.
- The algorithm sequentially checks each element until it finds a match or traverses the entire 2D array.
- The Java implementation showcases how to adapt Linear Search for multi-dimensional arrays.
Wanted to lean more please follow the directory: Array
In conclusion, understanding Linear Search in a 2D array in Java provides valuable insights into navigating multi-dimensional data structures. Experiment with different 2D arrays and target elements to solidify your grasp of this essential algorithm. Happy coding!
Latest posts:
- Rotate an Array by K Positions (Left and Right)Title: Rotate an Array by K Positions (Left and Right) Array rotation is one of those small, deceptively simple problems… Read more: Rotate an Array by K Positions (Left and Right)
- Reverse an Array in Groups of KTitle: Reverse an Array in Groups of K Reversing an array in groups of K is a classic interview question… Read more: Reverse an Array in Groups of K
- Reverse Half of an ArrayTitle: Reverse Half of an Array In this guide, you’ll learn how to reverse the first half of an integer… Read more: Reverse Half of an Array
- Reverse an Array using RecursionTitle: Reverse an Array using Recursion Reversing an array is one of the first problems every Java developer meets, and… Read more: Reverse an Array using Recursion
- Reverse an Array using a LoopTitle: Reverse an Array using a Loop Reversing an array is one of the first array manipulations every Java developer… Read more: Reverse an Array using a Loop
- Product of Even and Odd Elements in an ArrayTitle: Product of Even and Odd Elements in an Array In this guide, we will solve a classic beginner-friendly array… Read more: Product of Even and Odd Elements in an Array



