Sequential Search of Classical Algorithms

Event address: CSDN 21-day learning challenge

The biggest reason for learning is to get rid of mediocrity. One day earlier, there will be more splendor in life; one day later, one day more mediocrity.


1 Introduction

Recently, the editor is following the great god @一头小山猪 to learn algorithms. The purpose of learning is mainly to urge myself to learn. Algorithms are the foundation and need to be reviewed frequently. Therefore, the editor has compiled the learning content and used it as my own notes. I also hope that my own Blogs can help those in need.
Before learning, you need to have some basics, for example, what is an algorithm, how to evaluate the quality of an algorithm, etc., then I will review it for you in a very concise language.

2. Basic concepts of algorithms

算法In short, it is a step-by-step solution to the problem.
The efficiency of the algorithm is evaluated by 时间复杂度and .空间复杂度

  • Time complexity:
    It is the sum of the execution time of each piece of code. The common time complexities are (from low to high):
    insert image description here
  • Space complexity:
    the number of additional temporary variables or how many storage structures are required when the algorithm is running.

4. Sequential search

Today I mainly share it with my friends 顺序查找, so what is sequential search?
Sequential search is also called linear search. The basic idea is: start from one end of the linear list, and compare the elements recorded in the linear list with the given value in turn. If the elements in the linear list are equal to the given value, it means that the search is successful and returns The subscript of the element; otherwise, the search fails and -1 is returned.

4.1 Pseudocode

The pseudo code is as follows

//while循环
i = 1
while i <= A.length
	if A[i] == key
		return i
	i++
return -1
//或者for循环
i = 1
for (i = 1; i <= A.length; i++)
	if A[i] = key
		return i
return -1

For sequential search, the editor provides two languages

4.2 Java Edition

public class SequentialSearch {
    
    
    public static void main(String[] args) {
    
    
        int[] a = {
    
     11, 34, 20, 10, 12, 35, 41, 32, 43, 14 };
        int key = 10;
        int result = search(a, key);
        // System.out.println(result);
        if (result != -1) {
    
    
            System.out.println("10的下标为:" + result);
        } else {
    
    
            System.out.println("找不到该元素");
        }
    }

    private static int search(int[] a, int key) {
    
    
        for (int i = 0; i < a.length; i++) {
    
    
            if (a[i] == key) {
    
    
                return i;
            }
        }
        return -1;
    }
}

4.3 Python version

def sequent(alist,key):
    for i in range(len(alist)):
        if alist[i]==key:
            return i
    return None

if __name__ == '__main__':
    alist = [11, 34, 20, 10, 12, 35, 41, 32, 43, 14]
    key = 41
    result = sequent(alist,key)
    print(result)

4. Algorithm efficiency analysis

4.1 Time Complexity

  • Worst case
    The worst case is that the entire linear list is traversed and the target element is not found. The number of loops is related to N, so the time complexity is O(n).
  • In the best case,
    the target element is found by traversing the first element. So the time complexity is O(1).
  • Average case
    Based on the above two cases, it can be concluded that the time complexity of sequential search is O(n), which belongs to the slow search algorithm.

4.2 Space Complexity

The original collection is not changed when the algorithm is executed, only an additional variable is needed to control the index change, so the space complexity is O(1).


Conclusion: If you love your own value, you have to create value for the world.

Guess you like

Origin blog.csdn.net/weixin_42182599/article/details/126187450