-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinary_search.cpp
135 lines (123 loc) · 2.86 KB
/
binary_search.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include <bits/stdc++.h>
using namespace std;
int binarySearch(int array[], int x, int low, int high)
{
while (low <= high)
{
int mid = low + (high - low) / 2;
if (array[mid] == x)
return mid;
if (array[mid] < x)
low = mid + 1;
else
high = mid - 1;
}
return -1;
}
int main()
{
int n, x;
cout<<"Enter the number of elements?"<<endl;
cin >> n;
int array[n];
cout<<"Enter the elements?"<<endl;
for (int i = 0; i < n; i++)
cin >> array[i];
sort(array, array+n);
//for decending sort
// sort(arr, arr + n, greater<int>());
cout<<"After sorting in acending order:"<<endl;
for (int i = 0; i < n; i++)
cout<<array[i]<<" ";
cout<<"\nWhich element you want to seach?"<<endl;
cin >> x;
int result = binarySearch(array, x, 0, n - 1);
if (result == -1)
printf("Not found");
else
cout <<"Element is found at index: " <<result+1<< endl;
return 0;
}
/*
//Binary search using quick sorting
#include <bits/stdc++.h>
using namespace std;
int N, A[9999], temp;
int Partition(int A[], int p, int r)
{
int x = A[r], i = p - 1;
for (int j = p; j <= r - 1; j++)
{
if (A[j] <= x)
{
i = i + 1;
temp = A[i];
A[i] = A[j];
A[j] = temp;
}
}
temp = A[i + 1];
A[i + 1] = A[r];
A[r] = temp;
return i + 1;
}
void QuickSort(int A[], int p, int r)
{
if (p < r)
{
int q = Partition(A, p, r);
if (q > 1)
QuickSort(A, p, q - 1);
if (q < N)
QuickSort(A, q + 1, r);
}
}
int binarySearch(int A[], int p, int r, int key)
{
int left = p, right = r, mid;
while (left < right)
{
mid = (left + right) / 2;
if (A[mid] == key)
return mid;
else if (A[mid] < key)
left = mid + 1;
else
right = mid - 1;
}
if (A[mid] == key)
return mid;
else if (A[left] == key)
return left;
else if (A[right] == key)
return right;
else
return -1;
}
int main()
{
cout << "Enter the number of array size?" << endl;
cin >> N;
cout << "Enter Numbers: " << endl;
for (int i = 1; i <= N; i++)
cin >> A[i];
QuickSort(A, 1, N);
cout << "Quick Sorted: " << endl;
for (int i = 1; i <= N; i++)
{
if (i != 1)
cout<< A[i]<<" ";
else
cout<< A[i]<<" ";
}
cout << "\nEnter Value to Search: " << endl;
int X;
cin >> X;
int idx = binarySearch(A, 1, N, X);
if (idx == -1)
cout << "Not found. " << endl;
else
cout << "Found at index: " << (idx + 1) << endl;
return 0;
}
*/