-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray_sort_search.cpp
More file actions
60 lines (57 loc) · 1.22 KB
/
array_sort_search.cpp
File metadata and controls
60 lines (57 loc) · 1.22 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
#include <iostream>
int main()
{
int size = 0, i = 0,j = 0, min = 0, temp = 0;
std::cout << "Please enter the size of the array:" << std::endl;
std::cin >> size;
int *list = new int[size];
std::cout << "Kindly enter the array elements" << std::endl;
for(i = 0; i < size; i++)
{
std::cin >> list[i];
}
for(i = 0; i < size - 1; i++)
{
min = i;
for(j = i + 1; j < size; j++)
{
if(list[j] < list[min])
{
min = j;
}
}
temp = list[i];
list[i] = list[min];
list[min] = temp;
}
std::cout << "The sorted array is" << std::endl;
for(i = 0; i < size; i++)
{
std::cout << list[i] << std::endl;
}
int start = 0, end = size - 1, mid = 0, key = 0, pos = 0;
bool flag = false;
std::cout << "Enter the element you want to search" << std::endl;
std::cin >> key;
std::cout << key << std::endl;
while(start <= end)
{
mid = (start + end) / 2;
std::cout << mid << std::endl;
if(list[mid] == key)
{
pos = mid;
flag = true;
}
if(key > list[mid])
start = mid + 1;
if(key < list[mid])
end = mid - 1;
// mid = (start + end) / 2;
}
if(flag = true)
std::cout << "The element is found at " << pos << std::endl;
else
std::cout << "Element not found" << std::endl;
delete [] list;
}