-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearchingElementPosition.cpp
More file actions
59 lines (54 loc) · 1.54 KB
/
searchingElementPosition.cpp
File metadata and controls
59 lines (54 loc) · 1.54 KB
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
// Problem: Searching element in array using binary search by user.
#include <iostream>
// #include <algorithm>
using namespace std;
// C++ also have the built in sorting function in algorithm library
int SortArray (int arr[], int size) {
for(int i=0; i<size; i++){
for(int j=i+1; j<size; j++) {
if(arr[i] > arr[j]){
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
cout<< "\n" << "Array after sorting : ";
for(int i=0; i<size; i++) {
cout << arr[i ]<< " ";
}
return 0;
}
int FindUsingBinarySearch (int arr[], int start, int end, int target) {
while (start <= end) {
int mid = (start + end) /2;
if (arr[mid] == target) {
cout << "\n'" << target << "' element is found at index " << (mid) << endl;
exit (0);
}
else if (target > arr[mid]) {
start = mid + 1;
}
else if (target < arr[mid]) {
end = mid - 1;
}
}
cout << "Element not found in an array! ";
return 0;
}
int main() {
int size, arr[100], target;
cout << "Enter the size of an array : ";
cin >> size;
for ( int i=0; i<size; i++){
cout << " Value at index " << i+1 << " : ";
cin >> arr[i];
}
// Built-in sorting function
// sort (arr, arr+size);
cout << "\n" << "Enter the element to find in an array : ";
cin >> target;
SortArray (arr, size);
FindUsingBinarySearch(arr, 0, size , target);
return 0;
}