-
Notifications
You must be signed in to change notification settings - Fork 0
/
search_21BCE3982.cpp
53 lines (53 loc) · 1.32 KB
/
search_21BCE3982.cpp
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
46
47
48
49
50
51
52
53
#include <bits/stdc++.h>
using namespace std;
int linearSearch(int *a, int x, int n)
{
for (int i = 0; i < n; i++)
{
if (a[i] == x)
return i;
}
return -1;
}
int n;
int binarySearch(int *a, int low, int high, int x)
{
for (int i = 1; i < n; i++)
{
int j = i - 1;
int x = a[i];
while (j > -1 && a[j] > x)
{
a[j + 1] = a[j];
j--;
}
a[j + 1] = x;
}
if (low < high)
{
int mid = (low + high) / 2;
if (a[mid] == x)
return mid;
if (a[mid] < x)
return binarySearch(a, mid + 1, high, x);
else
return binarySearch(a, low, mid - 1, x);
}
return -1;
}
int main()
{
int a[] = {5, 13, 2, 25, 7, 17, 20, 8, 4, 45, 48, 12, 13, 10, 22, 11, 44, 99, 203, 17, 79, 46, 147, 58, 14};
n = sizeof(a) / sizeof(int);
int x;
cout << "Enter Search element : ";
cin >> x;
int lin_index = linearSearch(a, x, n);
int bin_index = binarySearch(a, 0, n - 1, x);
if (lin_index != -1)
cout << "Element found through linear search at " << lin_index << "\n";
if (bin_index != -1)
cout << "Element found through binary search at " << bin_index << "\n";
else if (lin_index == -1 || bin_index == -1)
cout << "Element not found";
}