forked from mishrahrishikesh/CPPExamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRealtiveSorting
More file actions
59 lines (48 loc) · 1.21 KB
/
RealtiveSorting
File metadata and controls
59 lines (48 loc) · 1.21 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
#include <bits/stdc++.h>
using namespace std;
vector<int> sorted(vector<int> a,vector<int> b,int n1,int n2){
vector <int> c;
// array a is sorted using labrary sorting function
sort(a.begin(),a.end());
for(int i=0;i<n2;i++){
for(int j=0;j<n1 && a[j]<=b[i];j++){
// elements of sorted a is entered to array c
// maintaing the element order as in b
if(a[j]==b[i]){
c.push_back(a[j]);
//clear the element pushed into c
a[j]=0;
}
}
}
// the elements that are not in b is in being entered to c
// in sorted manner as a is already sorted
for(int i=0;i<n1;i++)
if(a[i]!=0) //remaining elements of a
c.push_back(a[i]);
//return the output
return c;
}
int main() {
int n1,n2,u;
vector<int> :: iterator p; //iterator p
scanf("%d %d",&n1,&n2);
vector<int> a; //array a
vector<int> b;//array b
for(int j=0;j<n1;j++){
scanf("%d",&u);
// inputing elements of array a
a.push_back(u);
}
for(int j=0;j<n2;j++){
scanf("%d",&u);
// inputing elements of array b
b.push_back(u);
}
// implemented relative sorting function
vector<int> c=sorted(a,b,n1,n2);
for(p=c.begin();p!=c.end();p++)
printf("%d ",*p); // printing the sorted array
printf("\n");
return 0;
}