Java中的顺序搜索

A sequential search is a straight forward way to look for an element in a collection. This type of search uses a loop to go over each element one by one and see if it matches with the element we are looking for. The searching mechanism moves in a sequence, hence the name Sequential search.

顺序搜索是在集合中查找元素的直接方法。 这种搜索使用循环逐个遍历每个元素,并查看它是否与我们要查找的元素匹配。 搜索机制按顺序移动,因此命名为顺序搜索。

In case the data is sorted, we can reduce the number of lookups. We can stop the search when we exceed the target element. This is only possible in the case when the target element is not present.

如果对数据进行了排序,我们可以减少查找次数。 当我们超过目标元素时,我们可以停止搜索。 这仅在目标元素不存在的情况下才可能。

简单顺序搜索 (Simple Sequential Search)

In the case of unsorted data, the search method goes over all the elements. At each position, it compares the element at that position to the one we are looking for. If we find a match, we return true otherwise we continue our search. If we reach the end and no match is found, we return false.

对于未排序的数据,搜索方法将遍历所有元素。 在每个位置,它都会将该位置的元素与我们正在寻找的元素进行比较。 如果找到匹配项,则返回true,否则继续搜索。 如果到达末尾但找不到匹配项,则返回false。


package com.journaldev.java;

import java.util.ArrayList;
import java.util.Arrays;
public class Main {

    public static void main(String[] args) {
        int[] a = {3,2,4,5,3,2,7,6,4};
        boolean ans = contains(a,7);
        if(ans) {
            System.out.println("Number found");
        }
        else{
            System.out.println("Number not found");
        }

    }
    public static boolean contains(int[] a, int b){
        for (int i : a) {
            if (i==b){
                return true;
            }
        }
        return false;
    }
}
Number Found

排序数组中的顺序搜索 (Sequential Search in Sorted Array)

Sequential search over sorted data doesn’t always reduce the complexity or the number of lookups we need to perform. However in case, the element is not present, then we can stop our search the moment our search exceeds the target element.

对排序后的数据进行顺序搜索并不能总是降低我们需要执行的查找的复杂性或数量。 但是,如果该元素不存在,那么我们可以在搜索超出目标元素时停止搜索。


package com.journaldev.java;

import java.util.ArrayList;
import java.util.Arrays;
public class Main {

    public static void main(String[] args) {
        int[] a = {1,2,3,4,6,7,9};
        boolean ans = contains(a,5);
        if(ans) {
            System.out.println("Number found");
        }
        else{
            System.out.println("Number not found");
        }

    }
    public static boolean contains(int[] a, int b){
        for (int i : a) {
            System.out.println("Comparing with :" +i);
            if (i==b){
                return true;
            }
            else if(i>b){
                return false;
            }
        }
        return false;
    }
}
Sequential Search in sorted data

In this code, we print the number our search mechanism is comparing with. We can see that the search stops at 6 since 6 is already greater than 5 and if we move beyond 6 we will only come across numbers greater than 6.

在此代码中,我们将打印与搜索机制进行比较的数字。 我们可以看到搜索停止在6处,因为6已经大于5了,如果移到6以上,我们只会遇到大于6的数字。

An important thing to note over here is that the worst-case complexity still remains O(n). Since in the worst case, the element we are looking for could be the last element and we would need to go through the entire array.

这里要注意的重要一点是,最坏情况下的复杂度仍然是O(n)。 由于在最坏的情况下,我们要查找的元素可能是最后一个元素,因此我们需要遍历整个数组。

结论 (Conclusion)

A sequential search is the most basic search mechanism for searching in arrays. Apart from arrays, it is also common to use a sequential search for searching elements in the Linked List. The worst-case complexity of Sequential search is O(n) where n is the total number of elements.

顺序搜索是用于数组搜索的最基本的搜索机制。 除了数组之外,通常也使用顺序搜索来搜索链接列表中的元素。 顺序搜索的最坏情况复杂度是O(n),其中n是元素总数。

翻译自: https://www.journaldev.com/42282/sequential-search-in-java

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值