-
Notifications
You must be signed in to change notification settings - Fork 0
/
insertionsort.cpp
80 lines (64 loc) · 1.95 KB
/
insertionsort.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
69
70
71
72
73
74
75
76
77
78
79
80
#include <iostream>
using namespace std;
long long merge(int arr[], int start, int mid, int end) {
int aux_size = end - start + 1;
int aux[aux_size];
long long swaps = 0;
int pos_aux, pos_firstarray = start, pos_secondarray = mid + 1;
// compara os valores das duas arrays e coloca o menor primeiro na array auxiliar
for (pos_aux = 0; pos_aux < aux_size; pos_aux++) {
if (pos_firstarray > mid or pos_secondarray > end) break;
if (arr[pos_firstarray] <= arr[pos_secondarray]) {
aux[pos_aux] = arr[pos_firstarray];
pos_firstarray++;
}
else {
aux[pos_aux] = arr[pos_secondarray];
pos_secondarray++;
swaps += mid - pos_firstarray + 1;
}
}
// coloca os elementos que tiverem sobrado de alguma array
if (pos_firstarray > mid) {
for (int i = pos_secondarray; i <= end; i++) {
aux[pos_aux] = arr[i];
pos_aux++;
}
}
else {
for (int i = pos_firstarray; i <= mid; i++) {
aux[pos_aux] = arr[i];
pos_aux++;
}
}
// coloca os valores da array auxiliar na array principal
int counter = 0;
for (int i = start; i <= end; i++) {
arr[i] = aux[counter];
counter++;
}
return swaps;
}
long long mergesort(int arr[], int start, int end) {
long long swaps = 0;
if (start >= end) return 0;
int mid = start + (end - start) / 2;
swaps += mergesort(arr, start, mid);
swaps += mergesort(arr, mid + 1, end);
swaps += merge(arr, start, mid, end);
return swaps;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
int t; cin >> t;
while (t--) {
int n; cin >> n;
int arr [n];
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
cout << mergesort(arr, 0, n - 1) << '\n';
}
return 0;
}