-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy path07-May-2020 Array
More file actions
110 lines (109 loc) · 1.76 KB
/
07-May-2020 Array
File metadata and controls
110 lines (109 loc) · 1.76 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <bits/stdc++.h>
using namespace std;
void leftshift(int a[],int n)
{
int temp=a[0];
for(int i=0;i<n-1;i++)
{
a[i]=a[i+1];
}
a[n-1]=temp;
}
void print(int a[],int n)
{
for(int i=0;i<n;i++)
cout<<a[i]<<" ";
}
void leftrotate(int a[],int n, int k)
{
k=k%n;
int temp[k];
for(int i=0;i<k;i++)
{
temp[i]=a[i];
}
for(int i=0;i<n-k;i++)
{
a[i]=a[i+k];
}
int j=0;
for(int i=n-k;i<n;i++)
{
a[i]=temp[j];
j++;
}
}
void rightrotate(int a[],int n,int k)
{
k=k%n;
int temp[k];
for(int i=0;i<k;i++)
{
temp[i]=a[n-k+i];
}
for(int i=n-1;i>=k;i--)
{
a[i]=a[i-k];
}
for(int i=0;i<k;i++)
{
a[i]=temp[i];
}
}
int linearsearch(int a[],int n,int k)
{
for(int i=0;i<n;i++)
{
if(a[i]==k)
{
return i;
}
}
return -1;
}
int binarysearch(int a[],int n,int k)
{
int st=0;
int end=n-1;
while(st<=end)
{
// int mid=(st+end)/2;
int mid=st+(end-st)/2;
if(a[mid]==k)
{
return mid;
}
else{
if(a[mid]>k)
{
end=mid-1;
}
else{
st=mid+1;
}
}
}
return -1;
}
int main() {
int a[]={1,2,3,4,5,6,8,10,11,15};
int n=10;
int k=15;
int z=binarysearch(a,n,3);
int m=INT_MAX;
int c=INT_MIN;
// cout<<"\nPrinting Maximum and Minimum values "<<c<<" "<<m+2<<endl;
//int z=linearsearch(a,n,19);
if(z==-1)
{
cout<<"\nNot Found";
}
else
{
cout<<"\nFound at index "<<z;
}
cout<<endl;
// leftrotate(a,n,14);
rightrotate(a,n,31);
print(a,n);
}