Website Summary Files Commits Branches @commit @diff @stats
path: root / Sem. 06 / linearSearch.cpp
blob: d5d2ff1d561cc603986f8225ef1c28e0feb2b4d9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <vector>
using namespace std;


bool linearSearch(const int arr[], unsigned size, int el)
{
	for (unsigned i = 0; i < size; i++)
	{
		if (arr[i] == el)
			return true;
	}
	return false;
}

bool binarySearch(const int arr[], unsigned size, int searched)
{
	unsigned fromIndex = 0;
	unsigned toIndex = size - 1;

	while (fromIndex <= toIndex)
	{
		unsigned midIndex = fromIndex + (toIndex - fromIndex) / 2;

		if (arr[midIndex] == searched)
			return true;

		if (arr[midIndex] < searched)
			fromIndex = midIndex + 1;
		else
			toIndex = midIndex - 1;
	}

	return false;
}



int main()
{
	const unsigned SIZE = 8;
	int arr[]  { 1, 12, 33, 14,35,4,17,38};
	cout << linearSearch(arr, SIZE, 14);
}