-
Notifications
You must be signed in to change notification settings - Fork 1
/
insertion_sort.cpp
52 lines (45 loc) · 1.6 KB
/
insertion_sort.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
#include <iostream>
#include <utility>
using namespace std;
int MaxDzielnik(pair<int, int> x){
int max = 1;
for(int i = x.second/2; i > 1; i--){
if(x.second%i == 0){
max = i;
break;
}
}
return max;
}
void Sortuj(pair<int, int> tab[], int n){
pair<int ,int> temp;
for(int i = 1; i < n/2; i++){
temp = tab[i];
for(int j = i - 1; (j >= 0) && (tab[j].first >= temp.first); j--){
if(tab[j].first > temp.first){
tab[j + 1] = tab[j];
tab[j] = temp;
}
else if((tab[j].first == temp.first) && MaxDzielnik(tab[j]) < MaxDzielnik(temp)){
tab[j + 1]=tab[j];
tab[j] = temp;
}
}
}
}
int main(){
int liczba_ciagow;
int dlugosc_ciagu;
cin >> liczba_ciagow;
for(int j = 0; j < liczba_ciagow; j++){
cin >> dlugosc_ciagu;
pair<int,int> ciag[dlugosc_ciagu/2];
for(int i = 0; i < dlugosc_ciagu/2; i ++){
cin >> ciag[i].first >> ciag[i].second;
}
Sortuj(ciag, dlugosc_ciagu);
for(int i = 0; i < dlugosc_ciagu/2; i++)
cout << ciag[i].first << " " << ciag[i].second << " ";
}
return 0;
}