Skip to content

Commit 33dc19f

Browse files
committed
practice 09-Feb-2020
1 parent 39ebe8a commit 33dc19f

7 files changed

+270
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
int exponentialSearch(vector<int> v, int n, int item){
5+
int i=1;
6+
while(i<n && v[i]<item){
7+
i*=2;
8+
}
9+
int l=i/2, h=i, mid;
10+
while(l<=h){
11+
mid=(l+h)/2;
12+
if(v[mid]==item) return mid;
13+
else if(v[mid]>item) h=mid-1;
14+
else l=mid+1;
15+
}
16+
return -1;
17+
}
18+
19+
int main()
20+
{
21+
ios_base::sync_with_stdio(false);
22+
cin.tie(NULL);
23+
int n;
24+
cin>>n;
25+
vector<int> v;
26+
for(int i=0;i<n;i++){
27+
int ch;
28+
cin>>ch;
29+
v.push_back(ch);
30+
}
31+
int item;
32+
cin>>item;
33+
int index=exponentialSearch(v,n,item);
34+
if(index>-1) cout<<"Item found at index "<<index;
35+
else cout<<"Item not found";
36+
37+
return 0;
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
int interpolationSearch(vector<int> v, int n, int item){
5+
int l=0, h=n-1, pos;
6+
while(l<=h){
7+
pos= l+((item-v[l])/(v[h]-v[l])*(h-l));
8+
if(v[pos]==item) return pos;
9+
else if(v[pos]>item) h=pos-1;
10+
else l=pos+1;
11+
}
12+
return -1;
13+
}
14+
15+
int main()
16+
{
17+
ios_base::sync_with_stdio(false);
18+
cin.tie(NULL);
19+
int n;
20+
cin>>n;
21+
vector<int> v;
22+
for(int i=0;i<n;i++){
23+
int ch;
24+
cin>>ch;
25+
v.push_back(ch);
26+
}
27+
int item;
28+
cin>>item;
29+
int index=interpolationSearch(v,n,item);
30+
if(index>-1) cout<<"Item found at index "<<index;
31+
else cout<<"Item not found";
32+
33+
return 0;
34+
}

algorithms/searching/jump_search.cpp

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
int jumpSearch(vector<int> v, int n, int item){
5+
int m=sqrt(n);
6+
for(int i=0;i<n;i+=m){
7+
if(v[i]==item) return i;
8+
else if(v[i]>item){
9+
//Linear search
10+
for(int j=i-m;j<i;j++){
11+
if(v[j]==item) return j;
12+
}
13+
return -1;
14+
}
15+
}
16+
return -1;
17+
}
18+
19+
int main()
20+
{
21+
ios_base::sync_with_stdio(false);
22+
cin.tie(NULL);
23+
int n;
24+
cin>>n;
25+
vector<int> v;
26+
for(int i=0;i<n;i++){
27+
int ch;
28+
cin>>ch;
29+
v.push_back(ch);
30+
}
31+
int item;
32+
cin>>item;
33+
int index=jumpSearch(v,n,item);
34+
if(index>-1) cout<<"Item found at index "<<index;
35+
else cout<<"Item not found";
36+
37+
return 0;
38+
}

array/juggling_rotation.cpp

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
int gcd(int a, int b){
5+
if(b==0) return a;
6+
return gcd(b, a%b);
7+
}
8+
9+
void jugglingRotate(int a[], int n, int d){
10+
d%=n;
11+
int steps=gcd(n, d);
12+
for(int i=0;i<steps;i++){
13+
int tmp=a[i];
14+
int j=i;
15+
while(1){
16+
int k=j+d;
17+
if(k>=n) k-=n;
18+
if(i==k)
19+
break;
20+
a[j]=a[k];
21+
j=k;
22+
}
23+
a[j]=tmp;
24+
}
25+
for(int i=0;i<n;i++) cout<<a[i]<<" ";
26+
}
27+
28+
int main()
29+
{
30+
ios_base::sync_with_stdio(false);
31+
cin.tie(NULL);
32+
int n;
33+
cin>>n;
34+
int a[n];
35+
for(int i=0;i<n;i++) cin>>a[i];
36+
int d;
37+
cin>>d;
38+
jugglingRotate(a, n, d);
39+
40+
41+
return 0;
42+
}

array/rotated_search.cpp

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
int searchIt(int a[], int n, int x){
5+
int l=0, h=n-1, mid;
6+
while(l<=h){
7+
mid=l+(h-l)/2;
8+
if(a[mid]==x) return mid;
9+
else if(a[mid]<a[h]){
10+
if(x>a[mid] && x<=a[h]) l=mid+1;
11+
else h=mid-1;
12+
}
13+
else if(a[mid]>a[l]){
14+
if(x>=a[l] && x<a[mid]) h=mid-1;
15+
else l=mid+1;
16+
}
17+
}
18+
return -1;
19+
}
20+
21+
int main(){
22+
ios_base::sync_with_stdio(false);
23+
cin.tie(NULL);
24+
int t;
25+
cin>>t;
26+
while(t--){
27+
int n;
28+
cin>>n;
29+
int a[n];
30+
for(int i=0;i<n;i++) cin>>a[i];
31+
int x;
32+
cin>>x;
33+
cout<<searchIt(a,n,x)<<endl;
34+
}
35+
36+
return 0;
37+
}

array/sorted_rotated_min_search.cpp

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
int pivot(int a[], int n){
5+
int l=0, h=n-1, mid;
6+
if(a[l]<=a[h]) return -1;
7+
while(l<=h){
8+
mid=l+(h-l)/2;
9+
if(a[mid]>a[mid+1]) return mid;
10+
else if(a[mid]<a[mid-1]) return mid-1;
11+
else {
12+
if(a[l]<=a[mid]) l=mid+1;
13+
else h=mid-1;
14+
}
15+
}
16+
}
17+
18+
int minSearch(int a[], int n){
19+
int mini=pivot(a,n);
20+
mini=(mini+1)%n;
21+
return a[mini];
22+
}
23+
24+
int main(){
25+
ios_base::sync_with_stdio(false);
26+
cin.tie(NULL);
27+
cout.tie(NULL);
28+
int t;
29+
cin>>t;
30+
while(t--){
31+
int n;
32+
cin>>n;
33+
int a[n];
34+
for(int i=0;i<n;i++) cin>>a[i];
35+
cout<<minSearch(a,n)<<endl;
36+
}
37+
38+
39+
40+
return 0;
41+
}
+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
bool searchIt(int a[], int n, int x){
5+
int i;
6+
if(n<=1) return false;
7+
for(i=0;i<n-1;i++)
8+
if(a[i]>a[i+1]) break;
9+
int l=(i+1)%n;
10+
int h=i;
11+
while(l!=h){
12+
if(a[l]+a[h]==x) return true;
13+
else if(a[l]+a[h]>x){
14+
h=(n+h-1)%n;
15+
}
16+
else {
17+
l=(l+n+1)%n;
18+
}
19+
}
20+
return false;
21+
}
22+
23+
int main(){
24+
ios_base::sync_with_stdio(false);
25+
cin.tie(NULL);
26+
int t;
27+
cin>>t;
28+
while(t--){
29+
int n;
30+
cin>>n;
31+
int a[n];
32+
for(int i=0;i<n;i++) cin>>a[i];
33+
int x;
34+
cin>>x;
35+
searchIt(a,n,x)? cout<<"Pair exist" : cout<<"Pair doesn't exist";
36+
cout<<endl;
37+
}
38+
39+
return 0;
40+
}

0 commit comments

Comments
 (0)