-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfigure.cpp
74 lines (64 loc) · 1.26 KB
/
configure.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
#include<iostream>
using namespace std;
#define Size 100
int binarySearch(int arr[], int l, int r, int x);
void sort(int arr[], int n);
int main()
{
int arr[Size];
cout << "Enter how many numbers you want" << endl;
int n;
cin >> n;
cout << "Enter your numbers :" << endl;
for (int i = 0; i < n; i++)
{
cin >> arr[i];
}
sort(arr, n);
cout << "enter number you want to search:" << endl;
int x;
cin >> x;
int result = binarySearch(arr, 0, n - 1, x);
if (result == -1)
{
cout << "Element is not in your numbers" << endl;
}
else
{
cout << "Element index is :" << result + 1 << endl;
}
return 0;
}
//tabe bazgashti jost o hoye dodoyi
//index adade dade shode bargardande mishavad
int binarySearch(int arr[], int l, int r, int x)
{
if (r >= l)
{
int mid = l + (r - l) / 2;
if (arr[mid] == x)
return mid;
//jostojo dar samte chap
if (arr[mid] > x)
return binarySearch(arr, l, mid - 1, x);
//jostojo dar samte rast
return binarySearch(arr, mid + 1, r, x);
}
return -1;
}
//tabe baraye morattab sazi baraye jostojooye binary
void sort(int arr[], int n)
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n - 1 - i; j++)
{
if (arr[j] > arr[j + 1])
{
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}