mirror of
https://hub.njuu.cf/TheAlgorithms/C-Plus-Plus.git
synced 2023-10-11 13:05:55 +08:00
document linear search
This commit is contained in:
parent
2b57d1ff88
commit
5ab43ad039
@ -1,5 +1,18 @@
|
|||||||
|
/**
|
||||||
|
* \file
|
||||||
|
* \brief [Linear search
|
||||||
|
* algorithm](https://en.wikipedia.org/wiki/Linear_search)
|
||||||
|
*/
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Algorithm implementation
|
||||||
|
* \param [in] array array to search in
|
||||||
|
* \param [in] size length of array
|
||||||
|
* \param [in] key key value to search for
|
||||||
|
* \returns index where the key-value occurs in the array
|
||||||
|
* \returns -1 if key-value not found
|
||||||
|
*/
|
||||||
int LinearSearch(int *array, int size, int key) {
|
int LinearSearch(int *array, int size, int key) {
|
||||||
for (int i = 0; i < size; ++i) {
|
for (int i = 0; i < size; ++i) {
|
||||||
if (array[i] == key) {
|
if (array[i] == key) {
|
||||||
@ -10,6 +23,7 @@ int LinearSearch(int *array, int size, int key) {
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** main function */
|
||||||
int main() {
|
int main() {
|
||||||
int size;
|
int size;
|
||||||
std::cout << "\nEnter the size of the Array : ";
|
std::cout << "\nEnter the size of the Array : ";
|
||||||
|
Loading…
Reference in New Issue
Block a user