forked from OxyNett/1_184_2020
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClasswork_19.11.cpp
107 lines (98 loc) · 1.83 KB
/
Classwork_19.11.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
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
#include <iostream>
using namespace std;
void Buble(long[], long);
void Cout(long[], long);
void Random(long[], long);
void Shaker(long[], long);
void Swap(long[], long);
int main()
{
int const x = 100;
int size;
cout << "Size: "; cin >> size;
long a[x];
cout << "_____________________________________BubleSort_______________________________________________" << "\n\n" << endl;
Random(a, size);
Buble(a, size);
/*for (int i = 0; i < size; i++)
{
cout << a[i] << endl;
}
*/
Cout(a, size);
cout << " " << endl;
cout << "______________________________________ShakerSort_____________________________________________" << "\n\n" << endl;
long MyInt[x];
Random(MyInt, size);
Shaker(MyInt, size);
Cout(MyInt, size);
cout << " " << endl;
}
void Buble(long a[] , long size)
{
long i;
long j;
long x;
for (i = 0; i < size; i++)
{
for (j = size - 1; j > i; j--)
{
if ((a[j - 1]) > (a[j]))
{
x = a[j - 1];
a[j - 1] = a[j];
a[j] = x;
}
}
Cout(a, size);
cout << " " << "\n" << endl;
}
}
void Random(long a[], long size)
{
for (int i = 0; i < size; i++)
{
a[i] = 1.0 + 9 * rand() / (int)RAND_MAX;
}
}
void Cout(long a[], long size)
{
for (int i = 0; i < size; i++)
{
cout << a[i];
}
}
void Swap(long MyInt[], long i, long j)
{
long glass = MyInt[i];
MyInt[i] = MyInt[j];
MyInt[j] = glass;
}
void Shaker(long MyInt[], long size)
{
long left = 0;
long right = size - 1;
long count = 0;
while (left <= right)
{
for (int i = left; i < right; i++, count++)
{
if (MyInt[i] > MyInt[i + 1])
{
Swap(MyInt, i, i + 1);
}
}
right--;
for (int i = right; i > left; i--, count++)
{
if (MyInt[i - 1] > MyInt[i])
{
Swap(MyInt, i - 1, i);
}
}
left++;
Cout(MyInt, size);
cout << " " << "\n" << endl;
}
cout << "Number of comparisons: " << count <<"\n" << endl;
}