Algorithms_in_C 1.0.0
Set of algorithms implemented in C.
Loading...
Searching...
No Matches
sentinel_linear_search.c File Reference

Linear Search with Sentinel algorithm implementation More...

#include <stdio.h>
#include <assert.h>
Include dependency graph for sentinel_linear_search.c:

Functions

int sentinel_linear_search (int arr[], int len, int key)
 for IO operations
 
static void test ()
 Self-test implementations.
 
int main ()
 Main function.
 

Detailed Description

Linear Search with Sentinel algorithm implementation

This algorithm saves the last element of the array, then replaces it with the value to be found and sets it as the sentinel. When searching, compares each element with the sentinel. If the same, returns the index. If the index is the index of the sentinel, it means it was not found. Of course, if the value to be found is the last element, we return the index of the last element.

Author
Regan Yue Time Complexity: O(N)

Function Documentation

◆ main()

int main ( void  )

Main function.

Returns
0 on exit
76 {
77 test(); // run self-test implementations
78 return 0;
79}
static void test()
Self-test implementations.
Definition sentinel_linear_search.c:59
Here is the call graph for this function:

◆ sentinel_linear_search()

int sentinel_linear_search ( int  arr[],
int  len,
int  key 
)

for IO operations

for assert

Utility function to search for an element in the array and return the index of the element

The so-called "sentinel" is to use a special value as the boundary key of the array. One less judgment statement can be used. The purpose is to avoid checking whether the entire array is searched at each step in the search process, so as to improve the efficiency of the program. We can use the last value of the array as the "sentinel", the data storage index i starts from 0 and ends at len-1, then the position where the index of arr is n-1 indicates that there is no data temporarily, which is the "sentinel" key. If the last element of the array is equal to the key, directly return the index of the last element. Before setting the last element of the array as the key, we hand over the last element of the array to temp for temporary storage. Then go to the array to find the key. If the key is found, stop the search, and then compare the found element index with len-1. If it is equal, it means it was not found. If it is not equal, it is found.

Parameters
arrthis is an array containing elements
lenthis is the number of elements in the array
keythe value we want to search
Returns
i if found, otherwise -1 is returned.
36 {
37 if(key == arr[len-1]){
38 return len-1;
39 }
40
41 int temp = arr[len-1];
42 arr[len-1] = key;
43
44 int i = 0;
45 while (arr[len-1] != arr[i]) {
46 i++;
47 }
48
49 arr[len-1] = temp;
50
51 return i != len-1 ? i : -1;
52
53}

◆ test()

static void test ( void  )
static

Self-test implementations.

Returns
void
59 {
60 int n,i;
61 n = 5;
62 /* init array */
63 int arr[] = { 1, 2, 2, 6, 99, 100, 999 };
64
65 assert(sentinel_linear_search( arr, n, 1 )==0);
66 assert(sentinel_linear_search( arr, n, 2 )==1);
67 assert(sentinel_linear_search( arr, n, 6 )==3);
68 assert(sentinel_linear_search( arr, n, 101 )==-1);
69 printf("All test cases have successfully passed!\n");
70}
int sentinel_linear_search(int arr[], int len, int key)
for IO operations
Definition sentinel_linear_search.c:36
Here is the call graph for this function: