2020-06-20 00:04:56 +08:00
|
|
|
/**
|
|
|
|
* \file
|
|
|
|
* \brief [Linear search
|
|
|
|
* algorithm](https://en.wikipedia.org/wiki/Linear_search)
|
2022-01-27 23:06:44 +08:00
|
|
|
*
|
|
|
|
* @author Unknown author
|
|
|
|
* @author [Ritika Mukherjee](https://github.com/ritikaa17)
|
2020-06-20 00:04:56 +08:00
|
|
|
*/
|
2022-01-27 23:06:44 +08:00
|
|
|
|
2022-09-26 22:35:38 +08:00
|
|
|
#include <iostream> /// for IO operations
|
|
|
|
#include <cassert> /// for assert
|
2020-06-20 00:04:56 +08:00
|
|
|
|
|
|
|
/**
|
2022-01-27 23:06:44 +08:00
|
|
|
* \brief [Algorithm implementation for linear search]
|
2020-06-20 00:04:56 +08:00
|
|
|
* \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
|
|
|
|
*/
|
2022-09-26 22:35:38 +08:00
|
|
|
int LinearSearch(int *array, int size, int key)
|
2022-01-27 23:06:44 +08:00
|
|
|
{
|
2022-09-26 22:35:38 +08:00
|
|
|
for (int i = 0; i < size; ++i)
|
2022-01-27 23:06:44 +08:00
|
|
|
{
|
2022-09-26 22:35:38 +08:00
|
|
|
if (array[i] == key)
|
|
|
|
{
|
2020-06-20 00:04:56 +08:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-27 23:06:44 +08:00
|
|
|
/* We reach here only in case element is not present in array, return an invalid entry in that case*/
|
2020-06-20 00:04:56 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2022-01-27 23:06:44 +08:00
|
|
|
/**
|
|
|
|
* @brief Self-test implementations
|
|
|
|
* @returns void
|
|
|
|
*/
|
2022-09-26 22:35:38 +08:00
|
|
|
static void tests()
|
|
|
|
{
|
2022-01-27 23:06:44 +08:00
|
|
|
int size = 4;
|
2020-06-20 00:04:56 +08:00
|
|
|
int *array = new int[size];
|
2022-09-26 22:35:38 +08:00
|
|
|
for (int i = 0; i < size; i++)
|
|
|
|
{
|
2022-01-27 23:06:44 +08:00
|
|
|
array[i] = i;
|
2020-06-20 00:04:56 +08:00
|
|
|
}
|
|
|
|
|
2022-01-27 23:06:44 +08:00
|
|
|
assert(LinearSearch(array, size, 0) == 0);
|
|
|
|
assert(LinearSearch(array, size, 1) == 1);
|
|
|
|
assert(LinearSearch(array, size, 2) == 2);
|
2020-06-20 00:04:56 +08:00
|
|
|
|
2022-01-27 23:06:44 +08:00
|
|
|
size = 6;
|
2022-09-26 22:35:38 +08:00
|
|
|
for (int i = 0; i < size; i++)
|
|
|
|
{
|
2022-01-27 23:06:44 +08:00
|
|
|
array[i] = i;
|
2020-06-20 00:04:56 +08:00
|
|
|
}
|
|
|
|
|
2022-01-27 23:06:44 +08:00
|
|
|
assert(LinearSearch(array, size, 3) == 3);
|
|
|
|
assert(LinearSearch(array, size, 1) == 1);
|
|
|
|
assert(LinearSearch(array, size, 5) == 5);
|
|
|
|
|
|
|
|
std::cout << "All tests have successfully passed!\n";
|
|
|
|
delete[] array; // free memory up
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Main function
|
|
|
|
* @returns 0 on exit
|
|
|
|
*/
|
2022-09-26 22:35:38 +08:00
|
|
|
int main()
|
|
|
|
{
|
2022-01-27 23:06:44 +08:00
|
|
|
int mode = 0;
|
|
|
|
|
|
|
|
std::cout << "Choose mode\n";
|
|
|
|
std::cout << "Self-test mode (1), interactive mode (2): ";
|
|
|
|
|
|
|
|
std::cin >> mode;
|
|
|
|
|
2022-09-26 22:35:38 +08:00
|
|
|
if (mode == 2)
|
|
|
|
{
|
2022-01-27 23:06:44 +08:00
|
|
|
int size = 0;
|
2022-09-26 22:35:38 +08:00
|
|
|
std::cout << "\nEnter the size of the array [in range 1-30 ]: ";
|
2022-01-27 23:06:44 +08:00
|
|
|
std::cin >> size;
|
|
|
|
|
2022-09-26 22:35:38 +08:00
|
|
|
while (size <= 0 || size > 30){
|
|
|
|
std::cout << "Size can only be 1-30. Please choose another value: ";
|
2022-01-27 23:06:44 +08:00
|
|
|
std::cin >> size;
|
|
|
|
}
|
|
|
|
|
|
|
|
int *array = new int[size];
|
|
|
|
int key = 0;
|
|
|
|
|
|
|
|
// Input for the array elements
|
|
|
|
std::cout << "Enter the array of " << size << " numbers: ";
|
2022-09-26 22:35:38 +08:00
|
|
|
for (int i = 0; i < size; i++)
|
|
|
|
{
|
2022-01-27 23:06:44 +08:00
|
|
|
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 << "Number found at index: " << index << "\n";
|
2022-09-26 22:35:38 +08:00
|
|
|
}
|
2022-01-27 23:06:44 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
std::cout << "Array element not found";
|
|
|
|
}
|
|
|
|
delete[] array;
|
|
|
|
}
|
2022-09-26 22:35:38 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
tests(); // run self-test implementations
|
2022-01-27 23:06:44 +08:00
|
|
|
}
|
2020-06-20 00:04:56 +08:00
|
|
|
return 0;
|
|
|
|
}
|