Linear search algorithm
More...
#include <iostream>
◆ LinearSearch()
| int LinearSearch |
( |
int * |
array, |
|
|
int |
size, |
|
|
int |
key |
|
) |
| |
Algorithm implementation
- Parameters
-
| [in] | array | array to search in |
| [in] | size | length of array |
| [in] | key | key value to search for |
- Returns
- index where the key-value occurs in the array
-
-1 if key-value not found
16 {
17 for (int i = 0; i < size; ++i) {
18 if (array[i] == key) {
19 return i;
20 }
21 }
22
23 return -1;
24}
◆ main()
main function
27 {
28 int size;
29 std::cout <<
"\nEnter the size of the Array : ";
31
32 int *array = new int[size];
33 int key;
34
35
36 std::cout <<
"\nEnter the Array of " << size <<
" numbers : ";
37 for (int i = 0; i < size; i++) {
39 }
40
41 std::cout <<
"\nEnter the number to be searched : ";
43
45 if (index != -1) {
46 std::cout <<
"\nNumber found at index : " << index;
47 } else {
49 }
50
51 delete[] array;
52 return 0;
53}
int LinearSearch(int *array, int size, int key)
Definition: linear_search.cpp:16