linear_search.cpp File Reference

Linear search algorithm More...

`#include <iostream>`
Include dependency graph for linear_search.cpp:

## Functions

int | LinearSearch (int *array, int size, int key)
int | main ()

## Detailed Description

Linear search algorithm

## Function Documentation

## 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

{
for (int i = 0; i < size; ++i) {
if (array[i] == key) {
return i;
}
}
return -1;
}

## main()

int main | ( | void |  | ) |

main function

{
int size;
std::cout << "\nEnter the size of the Array : ";
std::cin >> size;
int *array = new int[size];
int key;
// Input array
std::cout << "\nEnter the Array of " << size << " numbers : ";
for (int i = 0; i < size; i++) {
std::cin >> array[i];
}
std::cout << "\nEnter the number to be searched : ";
std::cin >> key;
int index = LinearSearch(array, size, key);
if (index != -1) {
std::cout << "\nNumber found at index : " << index;
} else {
std::cout << "\nNot found";
}
delete[] array;
return 0;
}
std::cin
std::cout
LinearSearch
int LinearSearch(int *array, int size, int key)
Definition: linear_search.cpp:16