-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathquestion_5.cpp
More file actions
64 lines (64 loc) · 1.79 KB
/
question_5.cpp
File metadata and controls
64 lines (64 loc) · 1.79 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
/*
Given n 2-dimensional points (x1,y1),(x2,y2),...,(xn,yn), De-sign an algorithm that follows the ‘Divide-Conquer-Combine’ startegy to
(a) arrange then−pointsin an increasing order of thexcoordi-nates
(b) arrange then−pointsin an increasing order of theycoordi-nates
(c) arrange then−pointsin decreasing order of the value (x−coordinate+y−coordinate)/2) coordinates
*/
#include <iostream>
#include <vector>
using namespace std;
struct Point {
float x;
float y;
};
vector < Point > merge_by_x(vector < Point > & left_half,
vector < Point > & right_half) {
vector < Point > merged;
int i = 0;
int j = 0;
while (i < left_half.size() && j < right_half.size()) {
if (left_half[i].x <= right_half[j].x) {
merged.push_back(left_half[i]);
i++;
}
else {
merged.push_back(right_half[j]);
j++;
}
}
while (i < left_half.size()) {
merged.push_back(left_half[i]);
i++;
}
while (j < right_half.size()) {
merged.push_back(right_half[j]);
j++;
}
return merged;
}
vector < Point > merge_sort_by_x(vector < Point > & points) {
if (points.size() <= 1) {
return points;
}
int mid = points.size() / 2;
vector < Point > left_half(points.begin(), points.begin() + mid);
vector < Point > right_half(points.begin() + mid, points.end());
left_half = merge_sort_by_x(left_half);
right_half = merge_sort_by_x(right_half);
return merge_by_x(left_half, right_half);
}
void print_points(const std::vector < Point > & points) {
for (const Point & point: points) {
cout << "(" << point.x << ", " << point.y << ") ";
}
cout << std::endl;
}
int main() {
vector < Point > points = {{3,2},{1,4},{5,1},{2,3}};
cout << "Original Points: ";
print_points(points);
vector < Point > sorted_points = merge_sort_by_x(points);
cout << "Points Sorted by x-coordinate: ";
print_points(sorted_points);
return 0;
}