-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelecton Sort Binary Serach.cpp
More file actions
71 lines (70 loc) · 1.49 KB
/
Copy pathSelecton Sort Binary Serach.cpp
File metadata and controls
71 lines (70 loc) · 1.49 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
60
61
62
63
64
65
66
67
68
69
70
71
#include <bits/stdc++.h>
using namespace std;
void selectionSort(int arr[], int size)
{
for (int i = 0; i < size - 1; ++i)
{
int min_idx = i;
for (int j = i + 1; j < size; ++j)
{
if (arr[j] < arr[min_idx])
{
min_idx = j;
}
}
swap(arr[i], arr[min_idx]);
}
}
int binarySearch(const int arr[], int size, int target)
{
int left = 0;
int right = size - 1;
while (left <= right)
{
int mid = left + (right - left) / 2;
if (arr[mid] == target)
{
return mid;
}
else if (arr[mid] < target)
{
left = mid + 1;
}
else
{
right = mid - 1;
}
}
return -1;
}
int main()
{
int size, target;
cout << "Enter the size: ";
cin >> size;
int arr[size];
cout << "Enter " << size << " numbers: ";
for (int i = 0; i < size; ++i)
{
cin >> arr[i];
}
selectionSort(arr, size);
cout << "Sorted: ";
for (int i = 0; i < size; ++i)
{
cout << arr[i] << " ";
}
cout << endl;
cout << "Enter the interested number: ";
cin >> target;
int index = binarySearch(arr, size, target);
if (index != -1)
{
cout << "Interested number found at: " << index << endl;
}
else
{
cout << "Interested number not found." << endl;
}
return 0;
}