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
main
method, a 2D array (array2D
) is declared and initialized. - The target element (
targetElement
) is set to 6 for demonstration purposes. - The
linearSearch2D
method is called with the 2D array and the target element, and the result is stored in theresult
array. - 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:
- Merging Two Sorted Arrays in JavaTitle: Merging Two Sorted Arrays in Java Merging two sorted arrays into a single array is a common operation in… Read more: Merging Two Sorted Arrays in Java
- Frequency of Elements in an ArrayTitle: Frequency of Elements in an Array Using Java Counting the frequency of elements in an array is a fundamental… Read more: Frequency of Elements in an Array
- Calculate the sum of diagonals in a matrixTitle: Calculate the sum of diagonals in a matrix In this blog post, we’ll delve into the world of matrices… Read more: Calculate the sum of diagonals in a matrix
- Binary Search in JavaBinary search is a powerful algorithm used to efficiently find a target value in a sorted array. In this blog… Read more: Binary Search in Java
- Removing Duplicate Elements in Array using JavaRemoving Duplicate Elements in Array using Java Arrays are fundamental in Java, but duplicate elements can clutter your data. In… Read more: Removing Duplicate Elements in Array using Java
- Transpose a 2D Array in JavaTitle: Transpose a 2D Array in Java In Java, transposing a 2D array involves converting its rows into columns and… Read more: Transpose a 2D Array in Java