-
Notifications
You must be signed in to change notification settings - Fork 0
/
MergeSort.cpp
68 lines (52 loc) · 1.52 KB
/
MergeSort.cpp
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
#include "../include/MergeSort.h"
MergeSort::MergeSort(Vertex* array, int size) : size(size) {
mergeSort(0, size - 1, array);
}
MergeSort::~MergeSort() {}
void MergeSort::mergeSort(int low, int high, Vertex* array) {
if (low < high) {
int mid = low + (high - low) / 2;
mergeSort(low, mid, array);
mergeSort(mid + 1, high, array);
merge(low, mid, high, array);
}
}
void MergeSort::merge(int low, int mid, int high, Vertex* array) {
int leftSize = mid - low + 1;
int rightSize = high - mid;
// Criação de arrays temporários
Vertex* leftArray = new Vertex[leftSize];
Vertex* rightArray = new Vertex[rightSize];
// Copia os dados para os arrays temporários
for (int i = 0; i < leftSize; i++) {
leftArray[i] = array[low + i];
}
for (int j = 0; j < rightSize; j++) {
rightArray[j] = array[mid + 1 + j];
}
// Merge dos arrays temporários de volta ao array original
int i = 0, j = 0, k = low;
while (i < leftSize && j < rightSize) {
if (leftArray[i].color <= rightArray[j].color) {
array[k] = leftArray[i];
i++;
} else {
array[k] = rightArray[j];
j++;
}
k++;
}
// Copia os elementos restantes, se houver
while (i < leftSize) {
array[k] = leftArray[i];
i++;
k++;
}
while (j < rightSize) {
array[k] = rightArray[j];
j++;
k++;
}
delete[] leftArray;
delete[] rightArray;
}