6

Java 中顺序搜索(线性搜索)

 8 months ago
source link: https://www.jdon.com/71135.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Java 中顺序搜索(线性搜索)

顺序搜索,也称为线性搜索,是一种简单的搜索算法,用于查找列表或数组中的特定目标元素。搜索过程包括一一检查列表中的每个元素,直到找到所需的元素或到达列表末尾。下面是 Java 中顺序搜索的实现:

public class SequentialSearch {

//顺序搜索法
    public static int sequentialSearch(int[] arr, int target) {
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == target) {
                return i; // 如果找到目标,则返回索引
            }
        }
        return -1; // Return -1 if the target is not found
    }

public static void main(String[] args) {
        // Example usage
        int[] array = {4, 2, 7, 1, 9, 5, 6};
        int targetElement = 7;

// Perform sequential search
        int result = sequentialSearch(array, targetElement);

// Display the result
        if (result != -1) {
            System.out.println("Element " + targetElement + " found at index " + result);
        } else {
            System.out.println("Element " + targetElement + " not found in the array");
        }
    }
}

在此示例中,该sequentialSearch方法采用数组 ( arr) 和目标元素 ( target) 作为参数,并按顺序迭代数组。如果找到目标元素,则返回该元素所在的索引。如果没有找到目标,则返回-1。
该方法通过示例数组和目标元素main演示了该方法的用法。sequentialSearch然后将结果打印到控制台。

处理重复和多次出现
我们之前提供的顺序搜索的基本实现将找到目标元素在数组中的第一次出现。如果目标元素多次出现,则仅返回第一次出现的索引。我们可以修改该方法以返回索引列表:

import java.util.ArrayList;  
import java.util.List;  
public class SequentialSearch {  
// Sequential search method to find all occurrences  
    public static List<Integer> sequentialSearchAll(int[] arr, int target) {  
        List<Integer> occurrences = new ArrayList<>();  
        for (int i = 0; i < arr.length; i++) {  
            if (arr[i] == target) {  
            occurrences.add(i);  
            }  
        }  
    return occurrences;  
    }  
     public static void main(String[] args) {  
        int[] arr = { 10, 25, 5, 15, 30, 20, 15 };  
        int targetElement = 15;
      List<Integer> occurrences = sequentialSearchAll(arr, targetElement);  
      if (!occurrences.isEmpty()) {  
            System.out.println("Element found at indices: " + occurrences);  
        } else {  
            System.out.println("Element not found in the array.");  
        }  
    }  
}  

所提供的代码会在数组 arr 中搜索目标元素 targetElement(为 15)的所有出现次数。然后打印找到的所有出现点的索引。

目标元素 15 出现在数组 arr 的两个索引处--索引 3 和索引 6。因此,输出显示在索引 3 和 6 处找到了该元素。

性能以及何时使用顺序搜索
顺序搜索简单且易于实现。它适用于小型数组或数组未排序的情况。然而,对于大型数组或需要频繁搜索时,它可能不是最佳选择,因为其线性时间复杂度可能会导致性能下降。

时间复杂度分析
如前所述,顺序搜索的时间复杂度在最坏的情况下为 O(n),其中 n 是数组中元素的数量。这意味着搜索时间随着数组的大小线性增加。如果数组已排序,还有其他搜索算法(例如时间复杂度为 O(log n) 的二分搜索)可以执行得更快。

处理边缘情况和提高效率
虽然基本的顺序搜索对于小型未排序数组效果很好,但必须考虑一些边缘情况并提高较大数据集的效率。

1.处理空数组:
在原始实现中,如果数组为空,即使没有要搜索的元素,搜索方法仍将迭代循环。为了处理这种边缘情况,我们可以在方法的开头添加一个简单的检查:

public class SequentialSearch {  
    // Sequential search method  
    public static int sequentialSearch(int[] arr, int target) {  
        if (arr == null || arr.length == 0) {  
            return -1;  
        }  
        // Iterate through each element of the array  
        for (int i = 0; i < arr.length; i++) {  
            // If the target element is found, return its index  
            if (arr[i] == target) {  
                return i;  
            }  
        }  
        // If the target element is not found, return -1  
        return -1;  
    }  
    public static void main(String[] args) {  
        int[] arr = {10, 25, 5, 15, 30, 20};  
        int targetElement = 15;  
        int result = sequentialSearch(arr, targetElement);  
        if (result != -1) {  
            System.out.println("Element found at index: " + result);  
        } else {  
            System.out.println("Element not found in the array.");  
        }  
    }  
}  

2. 提前终止:
如果在数组中较早找到目标元素,我们可以立即终止搜索,而不是继续迭代其余元素。这可以通过修改循环条件来实现:

public class SequentialSearch {  
    // Sequential search method with early termination  
    public static int sequentialSearch(int[] arr, int target) {  
        for (int i = 0; i < arr.length; i++) {  
            if (arr[i] == target) {  
                return i; // Early termination if the target element is found  
            }  
        }  
        return -1;  
    }  
    public static void main(String[] args) {  
        int[] arr = {10, 25, 5, 15, 30, 20};  
        int targetElement = 15;  
        int result = sequentialSearch(arr, targetElement);  
        if (result != -1) {  
            System.out.println("Element found at index: " + result);  
        } else {  
            System.out.println("Element not found in the array.");  
        }  
    }  
}  

3. 增强型 For 循环:
在Java中,我们还可以使用增强的for循环(for-each循环)来简化搜索实现。此循环迭代数组的所有元素,无需显式索引:

public class SequentialSearch {  
    // Sequential search method using an enhanced for loop  
    public static int sequentialSearch(int[] arr, int target) {  
        int index = 0;  
        for (int element : arr) {  
            if (element == target) {  
                return index; // Return the index of the target element  
            }  
            index++;  
        }  
        return -1; // Return -1 if the target element is not found  
    }  
    public static void main(String[] args) {  
        int[] arr = {10, 25, 5, 15, 30, 20};  
        int targetElement = 15;  
        int result = sequentialSearch(arr, targetElement);  
        if (result != -1) {  
            System.out.println("Element found at index: " + result);  
        } else {  
            System.out.println("Element not found in the array.");  
        }  
    }  
}  

4.排序数组优化:
如果数组已排序,我们可以利用这一事实并优化搜索。我们可以使用二分搜索,而不是使用时间复杂度为 O(n) 的顺序搜索,对于排序数组来说,二分搜索的时间复杂度为 O(log n)。

import java.util.Arrays;  
public class BinarySearchOptimized {  
    // Binary search method optimized for sorted arrays  
    public static int binarySearch(int[] arr, int target) {  
        int index = Arrays.binarySearch(arr, target);  
        return index >= 0 ? index : -1;  
    }  
    public static void main(String[] args) {  
        int[] sortedArr = {5, 10, 15, 20, 25, 30};  
        int targetElement = 15;  
        int result = binarySearch(sortedArr, targetElement);  
        if (result != -1) {  
            System.out.println("Element found at index: " + result);  
        } else {  
            System.out.println("Element not found in the array.");  
        }  
    }  
}  

注意:使用 Fork-Join 进行顺序搜索可能不是最有效的方法,因为 Fork-Join 是为并行性而设计的,而顺序搜索本质上是一个顺序过程。

总之,顺序搜索是一种基本算法,可以作为理解搜索技术的起点。通过探索各种优化和权衡,我们深入了解算法效率的重要性以及针对不同场景选择最合适算法的必要性。

根据数据的特点和应用的具体要求选择合适的搜索算法至关重要。顺序搜索适用于小型未排序数组或需要进行几次搜索的情况。

但是,对于较大的数组或排序数组,应使用更高级的搜索算法(例如二分搜索)以获得更好的性能。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK