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
152
153
154
155
156
157
158
159
160
161
162
163
| #include <iostream>
#include <fstream>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#define VALUES 20000
using namespace std;
struct tnode
{
int field;
struct tnode *left;
struct tnode *right;
};
void treeprint(tnode *tree)
{
if (tree != NULL) {
treeprint(tree->left);
cout << tree->field << " ";
treeprint(tree->right);
}
}
struct tnode * addnode(int x, tnode *tree) {
if (tree == NULL)
{
tree = new tnode;
tree->field = x;
tree->left = NULL;
tree->right = NULL;
}
else
if (x < tree->field)
tree->left = addnode(x, tree->left);
else
tree->right = addnode(x, tree->right);
return(tree);
}
void freemem(tnode *tree)
{
if (tree != NULL)
{
freemem(tree->left);
freemem(tree->right);
delete tree;
}
}
class Matrix{
private:
int **Item;
int *Item2;
int Row;
int Col;
public:
Matrix();
Matrix(int R,int C,int Low,int Hi);
~Matrix();
void Out();
void massive1();
};
void Matrix::Out(){
cout<<endl;
for(int i=0;i<Row;i++){
for(int j=0;j<Col;j++){
cout<<Item[i][j]<<"\t";
}
cout<<endl;
}
}
Matrix::Matrix(int R,int C,int Low,int Hi){
Row=R;
Col=C;
Item=new int*[Row];
for(int i=0;i<Row;i++)Item[i]=new int[Col];
srand(time(0));
for(int i=0;i<Row;i++)
for(int j=0;j<Col;j++)
Item[i][j]=Low+rand()%(Hi-Low+1);
}
void Matrix::massive1()
{
int k = (Row / 2) * (Col / 2 + Col % 2); // k - длина массива
cout << endl << "одномерный массив: ";
std::cout << std::endl;
int* Item2;
Item2 = new int[k];
int index = 0;
for (int i = 0; i < (Row / 2); i++)
{
for (int j = (Col / 2); j < Col; j++)
{
Item2[index] = Item[i][j];
++index;
}
}
for (int i = 0; i < k; i++)
{
cout << Item2[i] << " "; //вывод массива Item2
}}
/*cout<<endl;
cout <<"отсортированный по убыванию массив:"<< endl;
struct tnode *root = 0;
for (int i = 0; i < k ; ++i)
{root = addnode(Item2[i], root);
}
treeprint(root);
freemem(root);
std::cout << Item2[index] << ' ';
}*/
//destructor
Matrix::~Matrix(){
for(int i=0;i<Row;i++) delete [] Item[i];
delete [] Item;
}
int main(){
Matrix X(10,10,-100,100);
X.Out();
X.massive1();
struct tnode *root = 0; // Объявляем структуру дерева
system("chcp 1251"); // переходим на русский язык в консоли
system("cls");
int a; // текущее значение узла
// В цикле вводим 8 узлов дерева
for (int i = 0; i< 8; i++)
{
cout << "Введите узел " << i + 1 << ": ";
cin >> a;
root = addnode(a, root); // размещаем введенный узел на дереве
}
treeprint(root); // выводим элементы дерева, получаем отсортированный массив
freemem(root); // удаляем выделенную память
cin.get(); cin.get();
int i, j;
clock_t start;
double duration;
int range[VALUES];
int buf;
for(i = 0; i < VALUES; ++i)
range[i] = rand();
start = clock();
for(i = 0; i < VALUES; ++i) {
for(j = VALUES - 1; j >= i; --j) {
if(range[j] > range[j-1]) {
buf = range[j-1];
range[j-1] = range[j];
range[j] = buf;
}
}
}
duration = (double)(clock() - start) / CLOCKS_PER_SEC;
cout<<endl;
printf("Время сортировки: %f sec\n", duration);
return 0;
} |