### Introduction The search algorithm is designed to find specific elements within a data structure. Common search algorithms include linear search, binary search, and hash search. Here is a brief introduction to the principles and implementations of these search algorithms: - **Linear Search**: Linear search is the simplest search algorithm. It starts from the beginning of the data structure and compares each element one by one until the target element is found or all elements have been traversed. The time complexity of linear search is O(n), where n represents the size of the data structure. - **Binary Search**: Binary search is applicable to ordered data structures. Each time, it divides the data structure into two halves and then determines the sub-interval to continue the search based on the comparison result between the target element and the middle element. The time complexity of binary search is O(log n). - **Hash Search**: Hash search constructs a hash function to map data into a hash table and then directly accesses the target element according to the hash value. The average time complexity of hash search is O(1). ***It is applied in the dict module using hash search***. ### Interface ```c int search_linear(void *array, int left, int right, void *target, SearchOPS *ops); int search_binary(void *array, int left, int right, void *target, SearchOPS *ops); ``` Both of these search algorithms are used in the same way. They require passing in the address of the data, the search interval of the array, the search target, and the operation set that depends on the data container. They return the index of the found element in the array (a negative value will be returned if the search fails). ### Testing - **Linear Search Test**: ```c static void test_linear(void) { int array[] = {6, 3, 1, 0, -2, 9}; SearchOPS ops; int target = 1; ops.addr = int_array_addr; ops.cmp = int_cmp; target = 9, printf("search %d, result %d\r\n", target, search_linear(array, 0, sizeof(array) / sizeof(array[0]) - 1, &target, &ops)); target = -2, printf("search %d, result %d\r\n", target, search_linear(array, 0, sizeof(array) / sizeof(array[0]) - 1, &target, &ops)); target = 0, printf("search %d, result %d\r\n", target, search_linear(array, 0, sizeof(array) / sizeof(array[0]) - 1, &target, &ops)); target = 1, printf("search %d, result %d\r\n", target, search_linear(array, 0, sizeof(array) / sizeof(array[0]) - 1, &target, &ops)); target = 3, printf("search %d, result %d\r\n", target, search_linear(array, 0, sizeof(array) / sizeof(array[0]) - 1, &target, &ops)); target = 6, printf("search %d, result %d\r\n", target, search_linear(array, 0, sizeof(array) / sizeof(array[0]) - 1, &target, &ops)); target = 5, printf("search %d, result %d\r\n", target, search_linear(array, 0, sizeof(array) / sizeof(array[0]) - 1, &target, &ops)); target = -9, printf("search %d, result %d\r\n", target, search_linear(array, 0, sizeof(array) / sizeof(array[0]) - 1, &target, &ops)); } ``` **Results**: ``` search 9, result 5 search -2, result 4 search 0, result 3 search 1, result 2 search 3, result 1 search 6, result 0 search 5, result -1 search -9, result -1 ``` In this test for linear search: 1. First, an integer array `array` is defined with several elements. 2. Then, a `SearchOPS` structure `ops` is created, and its members `addr` and `cmp` are initialized with appropriate functions related to the array address and comparison operations for integers. 3. Subsequently, multiple searches are performed with different target values. For each target value, the `search_linear` function is called, and the result (the index if found or a negative value if not found) is printed out. - **Binary Search Test**: ```c static void test_binary(void) { int array[] = {0, 3, 6, 10, 62, 99}; SearchOPS ops; int target = 10; ops.addr = int_array_addr; ops.cmp = int_cmp; target = 0, printf("search %d, result %d\r\n", target, search_binary(array, 0, sizeof(array) / sizeof(array[0]) - 1, &target, &ops)); target = 3, printf("search %d, result %d\r\n", target, search_binary(array, 0, sizeof(array) / sizeof(array[0]) - 1, &target, &ops)); target = 6, printf("search %d, result %d\r\n", target, search_binary(array, 0, sizeof(array) / sizeof(array[0]) - 1, &target, &ops)); target = 10, printf("search %d, result %d\r\n", target, search_binary(array, 0, sizeof(array) / sizeof(array[0]) - 1, &target, &ops)); target = 62, printf("search %d, result %d\r\n", target, search_binary(array, 0, sizeof(array) / sizeof(array[0]) - 1, &target, &ops)); target = 99, printf("search %d, result %d\r\n", target, search_binary(array, 0, sizeof(array) / sizeof(array[0]) - 1, &target, &ops)); target = 5, printf("search %d, result %d\r\n", target, search_binary(array, 0, sizeof(array) / sizeof(array[0]) - 1, &target, &ops)); target = -9, printf("search %d, result %d\r\n", target, search_binary(array, 0, sizeof(array) / sizeof(array[0]) - 1, &target, &ops)); } ``` **Results**: ``` search 0, result 0 search 3, result 1 search 6, result 2 search 10, result 3 search 62, result 4 search 99, result 5 search 5, result -1 search -9, result -1 ``` For the binary search test: 1. An ordered integer array `array` is defined. 2. Similar to the linear search test, the `SearchOPS` structure `ops` is initialized. 3. Different target values are used to call the `search_binary` function one by one, and the corresponding search results are printed. The results show that when the target exists in the ordered array, the correct index is returned, and when the target doesn't exist, a negative value is returned, which verifies the functionality of the binary search algorithm.