-
Notifications
You must be signed in to change notification settings - Fork 0
/
1task.cpp
80 lines (76 loc) · 1.36 KB
/
1task.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 "conio.h"
#include "iostream"
#include "stack"
using namespace std;
class ST
{
public:
stack<int> mainarray;
stack<int> auxiliary;
int i;
int m;
void set( int x )
{
for( i = 0; i < x; i++ )
{
mainarray.push(rand()%100);
cout << mainarray.top() << " ";
}
cout << endl;
};
void sort( int x )
{
for( i = 0; i < x; i++)
{
m=mainarray.top();
mainarray.pop();
while( !mainarray.empty() )
{
if( m <= mainarray.top() )
{
auxiliary.push( m );
m=mainarray.top();
mainarray.pop();
}
else
{
auxiliary.push( mainarray.top() );
mainarray.pop();
}
}
mainarray.push( m );
while( !auxiliary.empty() )
{
mainarray.push( auxiliary.top() );
auxiliary.pop();
}
}
while( !mainarray.empty() )
{
cout << mainarray.top() << " ";
mainarray.pop();
}
};
};
int main()
{
setlocale( LC_ALL, "Russian" );
ST obj;
cout << "Введите значение х (количество элементов в первом массиве): " << endl;
int x;
cin >> x;
if(x <= 0)
{
cout << "х должен быть положительным числом! " << endl;
}
else
{
cout << endl;
cout << "Начальный массив: " << endl;
obj.set(x);
cout << "Массив после сортировки: " << endl;
obj.sort(x);
}
getch();
return 0;
}