-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsorting_array_pair.cpp
More file actions
60 lines (47 loc) · 1.37 KB
/
sorting_array_pair.cpp
File metadata and controls
60 lines (47 loc) · 1.37 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
#include<iostream>
using namespace std;
/*
NOTE:
In case of array, while passing reference we don't need to specify
using "&" before variable, but we put "[]" after variable.
In function, we should pass the size of array alongwith the array.
Here we can pass reference using "&" before variable.
For sorting based on FIRST elements we can use sort function
similar to the case of array, but for sorting based on SECOND
elements we need user defined function [Here, using bubble sort]
*/
void print(pair<int, float>pp[], int &n)
{
cout << endl;
for(int i=0; i<n ; i++)
cout << pp[i].first << " "<<pp[i].second<<endl;
cout << endl;
}
void sort2nd(pair<int,float>pp[], int &n)
{
for(int i=0 ; i< n-1 ; i++)
{
for(int j=i+1 ; j< n; j++)
{
if(pp[i].second>pp[j].second)
{
swap(pp[i].second, pp[j].second);
swap(pp[i].first, pp[j].first);
}
}
}
}
int main()
{
int n;
cin >> n;
pair<int, float> pp[n];
for(int i=0; i<n ; i++)
cin >> pp[i].first>> pp[i].second ;
print(pp,n);// print array pair the way input has been taken.
sort(pp+0,pp+n); // sort array pair based on FIRST elements.
print(pp,n); // sorted print
sort2nd(pp,n); //sort array pair based on SECOND elements.
print(pp,n); // sorted print
return 0;
}