-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSEARCHING.cpp
More file actions
73 lines (70 loc) · 1.6 KB
/
SEARCHING.cpp
File metadata and controls
73 lines (70 loc) · 1.6 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
72
73
#include<iostream>
#include<stdlib.h>
#include<math.h>
#include<time.h>
using namespace std;
int linsearch(int arr[],int s,int pos,int key)
{
int kpos
if(pos<size)
{
if(arr[pos]==key)
return pos;
kpos=linsearch(arr,s,pos+1,key);
return kpos;
}
return -1;
}
int binsearch(int arr[],int low,int high,int k)
{
int mid,kpos;
if(low<=high)
{
mid=(low+high)/2;
if(k==arr[mid])
return mid;
if(k<arr[mid])
kpos=binsearch(arr,low,mid-1,k);
if(k>arr[mid])
kpos=binsearch(arr,mid+1,high,k);
return kpos;
}
return -1;
}
void bsort(int arr[],int n)
{
int temp;
for(int i=0;i<n;i++)
for(int j=0;j<n-1;j++)
if(arr[j]>arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
int main()
{
srand(time(0));
int *arr,key,n,res,choice;
cout<<"Enter the number of elements:";
cin>>n;
arr=new int[n];
for(int i=0;i<n;i++)
arr[i]=((rand()%(20000-0+1))+0);
cout<<"Array elements:"<<endl;
for(int i=0;i<n;i++)
cout<<arr[i]<<" ";
bsort(arr,n);
cout<<endl<<endl<<endl<<"Sorted array:";
for(int i=0;i<n;i++)
cout<<arr[i]<<" ";
cout<<endl<<"Enter the key element between 0 and 20000:";
cin>>key;
res=binsearch(arr,0,n-1,key);
if(res==-1)
cout<<"Key not found"<<endl;
else
cout<<"Key found at position:"<<res+1<<endl;
return 0;
}