-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add the Sentinel Search algorithm (#883)
* Create sentinel_linear_search.c This is a search algorithm that uses sentinels. * Update sentinel_linear_search.c Added some notes and made some changes * updating DIRECTORY.md * Apply suggestions from code review thx u Co-authored-by: David Leal <[email protected]> * Update sentinel_linear_search.c * Apply suggestions from code review Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: David Leal <[email protected]>
- Loading branch information
1 parent
c56b967
commit 3cfdbb0
Showing
2 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/** | ||
* @file | ||
* @brief [Linear Search with Sentinel](https://en.wikipedia.org/wiki/Linear_search#With_a_sentinel) algorithm implementation | ||
* @details | ||
* 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](https://github.com/ReganYue) | ||
* Time Complexity: O(N) | ||
*/ | ||
|
||
#include <stdio.h> /// for IO operations | ||
#include <assert.h> /// for assert | ||
|
||
/** | ||
* @brief Utility function to search for an element in the array and return the index of the element | ||
* @details | ||
* 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. | ||
* @param arr this is an array containing elements | ||
* @param len this is the number of elements in the array | ||
* @param key the value we want to search | ||
* @return i if found, otherwise -1 is returned. | ||
*/ | ||
int sentinel_linear_search( int arr[], int len, int key ){ | ||
if(key == arr[len-1]){ | ||
return len-1; | ||
} | ||
|
||
int temp = arr[len-1]; | ||
arr[len-1] = key; | ||
|
||
int i; | ||
for(i=0;arr[len-1]!=arr[i];i++){ | ||
if(i==len-1){ | ||
This comment has been minimized.
Sorry, something went wrong. |
||
break; | ||
} | ||
} | ||
|
||
arr[len-1] = temp; | ||
|
||
return i != len-1 ? i : -1; | ||
|
||
} | ||
|
||
/** | ||
* @brief Self-test implementations | ||
* @returns void | ||
*/ | ||
static void test(){ | ||
int n,i; | ||
n = 5; | ||
/* init array */ | ||
int arr[] = { 1, 2, 2, 6, 99, 100, 999 }; | ||
|
||
assert(sentinel_linear_search( arr, n, 1 )==0); | ||
assert(sentinel_linear_search( arr, n, 2 )==1); | ||
assert(sentinel_linear_search( arr, n, 6 )==3); | ||
assert(sentinel_linear_search( arr, n, 101 )==-1); | ||
printf("All test cases have successfully passed!\n"); | ||
} | ||
|
||
/** | ||
* @brief Main function | ||
* @returns 0 on exit | ||
*/ | ||
int main(){ | ||
test(); // run self-test implementations | ||
return 0; | ||
} |
Isn't the point of sentinel search literally to remove this comparison, as key will always equal key in the last iteration?