Skip to content

Commit d67d450

Browse files
authored
Merge branch 'master' into my-algo-contribution
2 parents e0b364a + 0febbf0 commit d67d450

File tree

1 file changed

+25
-17
lines changed

1 file changed

+25
-17
lines changed

search/linear_search.cpp

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
* @author [Ritika Mukherjee](https://github.com/ritikaa17)
88
*/
99

10-
#include <iostream> /// for IO operations
11-
#include <cassert> /// for assert
10+
#include <iostream> /// for IO operations
11+
#include <cassert> /// for assert
1212

1313
/**
1414
* \brief [Algorithm implementation for linear search]
@@ -18,11 +18,12 @@
1818
* \returns index where the key-value occurs in the array
1919
* \returns -1 if key-value not found
2020
*/
21-
int LinearSearch(int *array, int size, int key)
21+
int LinearSearch(int *array, int size, int key)
2222
{
23-
for (int i = 0; i < size; ++i)
23+
for (int i = 0; i < size; ++i)
2424
{
25-
if (array[i] == key) {
25+
if (array[i] == key)
26+
{
2627
return i;
2728
}
2829
}
@@ -35,10 +36,12 @@ int LinearSearch(int *array, int size, int key)
3536
* @brief Self-test implementations
3637
* @returns void
3738
*/
38-
static void tests() {
39+
static void tests()
40+
{
3941
int size = 4;
4042
int *array = new int[size];
41-
for (int i = 0; i < size; i++) {
43+
for (int i = 0; i < size; i++)
44+
{
4245
array[i] = i;
4346
}
4447

@@ -47,7 +50,8 @@ static void tests() {
4750
assert(LinearSearch(array, size, 2) == 2);
4851

4952
size = 6;
50-
for (int i = 0; i < size; i++) {
53+
for (int i = 0; i < size; i++)
54+
{
5155
array[i] = i;
5256
}
5357

@@ -63,21 +67,23 @@ static void tests() {
6367
* @brief Main function
6468
* @returns 0 on exit
6569
*/
66-
int main() {
70+
int main()
71+
{
6772
int mode = 0;
6873

6974
std::cout << "Choose mode\n";
7075
std::cout << "Self-test mode (1), interactive mode (2): ";
7176

7277
std::cin >> mode;
7378

74-
if (mode == 2) {
79+
if (mode == 2)
80+
{
7581
int size = 0;
76-
std::cout << "\nEnter the size of the array: ";
82+
std::cout << "\nEnter the size of the array [in range 1-30 ]: ";
7783
std::cin >> size;
7884

79-
while ((size <= 1) || (size >= 30)) {
80-
std::cout << "Size cannot be less than zero. Please choose another value: ";
85+
while (size <= 0 || size > 30){
86+
std::cout << "Size can only be 1-30. Please choose another value: ";
8187
std::cin >> size;
8288
}
8389

@@ -86,7 +92,8 @@ int main() {
8692

8793
// Input for the array elements
8894
std::cout << "Enter the array of " << size << " numbers: ";
89-
for (int i = 0; i < size; i++) {
95+
for (int i = 0; i < size; i++)
96+
{
9097
std::cin >> array[i];
9198
}
9299

@@ -97,15 +104,16 @@ int main() {
97104
if (index != -1)
98105
{
99106
std::cout << "Number found at index: " << index << "\n";
100-
}
107+
}
101108
else
102109
{
103110
std::cout << "Array element not found";
104111
}
105112
delete[] array;
106113
}
107-
else {
108-
tests(); // run self-test implementations
114+
else
115+
{
116+
tests(); // run self-test implementations
109117
}
110118
return 0;
111119
}

0 commit comments

Comments
 (0)