-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10.cpp
72 lines (62 loc) · 1.8 KB
/
10.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
#include <iostream>
#include <vector>
#include <fstream>
#include <cmath>
using namespace std;
ifstream in("1.txt");
ofstream out("2.txt");
vector<vector<int>> create(int n){
vector<vector<int>> a(n, vector<int>(n));
for (int i = 0; i < n; ++i){
for (int j = 0; j < n; ++j)
in >> a[i][j];
}
return a;
}
void shell_step_3_nch(vector<int>& x, int n, int k){ //для четных строк (вводим вектор, размер, максимальный шаг)
int step = pow(3, k), d = n;
while (step >= 1){
for (int i = 0; i < d - step;i++) {
int j = i;
while (j >= 0 and x[j] > x[j + step]) {
swap(x[j], x[j + step]);
j--;
}
}
k -= 1;
step = pow(3,k);
}
}
void shell_step_3_ch(vector<int>& x, int n, int k){ //для четных строк (вводим вектор, размер, максимальный шаг)
int step = pow(3, k), d = n;
while (step >= 1){
for (int i = 0; i < d - step;i++) {
int j = i;
while (j >= 0 and x[j] < x[j + step]) {
swap(x[j], x[j + step]);
j--;
}
}
k -= 1;
step = pow(3,k);
}
}
int main (){
int n; cin >> n;
vector <vector<int>> a(n, vector<int> (n));
a = create(n);
//определим максимальный шаг для Шелла с шагом 3 в степени i
int k = 0;
while (pow(3, k) < n) {
k += 1;
}
k -= 1;
for(int i = 0; i < n; i++){
if (i % 2 == 0) shell_step_3_ch(a[i], n, k);
else shell_step_3_nch(a[i], n, k);
}
for (int i = 0; i < n; ++i, out << endl){
for (int j = 0; j < n; ++j)
out << a[i][j] << " ";
}
}