include stdafx include iostream include cstdlib include ctime using na

 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
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int BinSearch(const int* arr, int count, int key);
void s_vb(int a[], int n);
int main()
{
srand(time_t(NULL));
setlocale(0,"");
int n;
cout << "Input elements of array: ";
cin>>n;
int *arr = new int[n];
for(int i=0;i<n;i++)
arr[i]=rand()%10;
int key;
cout << "key: ";
cin >> key;
for(int i=0;i<n;i++){
cout << arr[i] << ' ';
}cout <<'\n';
if(BinSearch(arr, n, key) != -1)
cout << "Yes" << endl;
else cout << "No" << endl;
int k=BinSearch(arr,n,key);
int *b=new int [k];
for(int i=0;i<k;i++)
b[i]=arr[i];
int *c=new int [n-k];
for(int i=0;i<(n-k);i++)
c[i]=arr[k+i];
for(int i=0;i<k;i++)
cout <<b[i]<<' ';
cout <<'\n';
s_vb(c,n-k);
for(int i=0;i<(n-k);i++)
cout <<c[i]<<' ';
cout <<'\n';
int i=0;
for(;i<k;i++)
arr[i]=b[i];
for(int t=0;i<n;i++,t++)
arr[i]=c[t];
for(int t=0;t<n;t++)
cout <<arr[t]<<' ';
cout <<'\n';
delete [] arr;
return 0;
}
int BinSearch(const int* arr, int count, int key)
{
int l = 0; // bottom
int u = count - 1; // top
while (l <= u) {
int m = (l + u) / 2;
if (arr[m] == key) return m;
if (arr[m] < key) l = m + 1;
if (arr[m] > key) u = m - 1;
}
return -1;
}
void s_vb(int a[], int n) {
int imin,i,j,t;
for(i=0; i<n-1; i++) {
imin=i;
for(j=i+1; j<n; j++)
if (a[imin]>a[j])
imin=j;
if (imin != i) {
t = a[imin];
a[imin] = a[i];
a[i] = t;
}
}
}