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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
| #include <fstream>
#include <stdlib.h>
#include <iostream>
#include <time.h>
#include <iomanip>
#include <conio.h>
#include <stdio.h>
#define maxn 100
struct data {
int vek;}
arr[10];
void printMatrix (int *x, const int S);
int load (struct data *t, char *filename);
void write (struct data *t, char *filename, int k);
void pyramidal (struct data *t, const int k);
void build (const int k, int r, int *x);
void sort_pyr (const int k, int *x);
void shifting (int left, int right, int *x);
void exchange (int i, int j, int *x);
//void merge(int *y, int l, int r);
using namespace std;
int main() {
const int N=10;
int x[N], i, a, b;
srand (time(NULL)); //Для генерації псевдовипадкових чисел використовується системний час
cout << "Enter a: "; //Вибір нижньої межі генерованих чисел
cin >> a;
cout << "Enter b: "; //Вибір верхньої межі
cin >> b;
for (i=0; i<N; i++) {
x[i]=a+rand()%(b-a+1); //Заповнення масиву псевдовипадковими числами згідно вказаних меж
}
char y[maxn]="algorithm.txt";
write(x, y, N);
cout << "Entered array X: ";
printMatrix(x, N);
cout << "Piramidal sorted array: ";
printMatrix(x, N);
//cout << "Merge sorted array: ";
/*merge (y, 0, N-1);
printMatrix (y, N);*/
system ("pause");
return 0; }
void printMatrix(int *x, const int S) { //Функція виведення масиву на екран
cout << endl;
for (int i=0; i<S; i++) {
cout << setw (5) << x[i]; }
cout << endl << endl; }
int load (struct data *t, char *filename) {
int i=0;
FILE *f=fopen (filename, "r");
if (f==NULL) {
puts ("Error");
return 0; }
while (!feof(f)) {
fscanf (f, "%i", t[i].vek);
i++; }
fclose(f);
return i;}
void write (truct data *t, char *filename, const int k) {
FILE *f=fopen (filename, "w");
if (f) {
for (int i=0; i<k; i++) {
bool result = fwrite(&x[i], sizeof(int), 1, f);
if (!result) {
cout << "Writing complete" << endl;}}}
fclose (f);
}
void pyramidal (struct data *t, const int k) { //Опис функції пірамідального сортування
int i;
char filename[maxn]=("algorithm.txt");
int x[k];
for (i=0; i<k; i++) {
x[i]=t[i].vek;}
build (k/2+1, k-1, x);
sort_pyr (k-1, x);
}
void build (const int k, int r, int *x) { //Побудова піраміди
shifting (k, r, x);
if (k>0) {
build (k-1, r, x);}}
void sort_pyr (const int k, int *x) { //Сортування піраміди
exchange (0, k, x);
shifting (0, k-1, x);
if (k>1) {
sort_pyr (k-1,x); }}
void shifting (int left, int right, int *x) { //Просіювання елементів
int q, p, h;
q=2*left+1;
p=q+1;
if (q <=right) {
if (p<=right && x[p] >x[q]) {
q=p;}
if (x[left]<x[q]){
exchange (left, q, x);
shifting (q, right, x);}}}
void exchange (int i, int j, int *x) { //Процедура обміну двох елементів
int tmp;
tmp=x[i];
x[i]=x[j];
x[j]=tmp; }
/*void merge(int *y, int l, int r) { //Сортування злиттям
if (r == l)
return;
if (r - l == 1) {
if (y[r] < y[l])
swap(y[r], y[l]);
return;
}
int m = (r + l) / 2;
merge(y, l, m);
merge(y, m + 1, r);
int buf[maxn];
int xl = l;
int xr = m + 1;
int cur = 0;
while (r - l + 1 != cur) {
if (xl > m)
buf[cur++] = y[xr++];
else if (xr > r)
buf[cur++] = y[xl++];
else if (y[xl] > y[xr])
buf[cur++] = y[xr++];
else buf[cur++] = y[xl++];
}
for (int i = 0; i < cur; i++)
y[i + l] = buf[i];
}
*/ |