forked from Maria02179/mp1-2020-382003-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sort.c
136 lines (120 loc) · 1.77 KB
/
Sort.c
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
#include "stdio.h"
#include "stdlib.h"
#include "stdbool.h"
void print(int B[], int n);
void BubbleSort1(int A[], int n)
{
int i, j;
int tmp;
for (i = 0; i < n; i++)
{
for (j = 0; j < n - i - 1; j++)
{
if (A[j] > A[j + 1])
{
tmp = A[j];
A[j] = A[j + 1];
A[j + 1] = tmp;
}
}
}
}
void BubbleSort(int A[], int n)
{
int i, j;
int tmp;
bool wasSwap;
for (i = 0; i < n; i++)
{
wasSwap = false;
for (j = 0; j < n - i - 1; j++)
{
if (A[j] > A[j + 1])
{
tmp = A[j];
A[j] = A[j + 1];
A[j + 1] = tmp;
wasSwap = true;
}
}
if (!wasSwap) // wasSwap == false
{
break;
}
print(A, n);
}
printf("i = %d\n", i);
}
void randArray(int B[], int n, int a, int b)
{
int i;
for (i = 0; i < n; i++)
B[i] = rand() % (b - a) + a;
}
void sortedArray(int B[], int n)
{
int i; int tmp;
for (i = 0; i < n; i++)
B[i] = i;
tmp = B[n - 2];
B[n - 2] = B[n - 1];
B[n - 1] = tmp;
}
void print(int B[], int n)
{
int i;
for (i = 0; i < n; i++)
printf("%d ", B[i]);
printf("\n");
}
s
void menu()
{
printf("\nMENU:\n");
printf("1. Input array\n");
printf("2. Print array\n");
printf("3. BubbleSort\n");
printf("0. Exit\n");
}
void main()
{
int B[10];
int n = 10;
int t = 10;
bool wasInput = false;
printf("start\n");
srand(1000);
while (t != 0)
{
menu();
scanf_s("%d", &t);
switch (t)
{
case 1: {
//sortedArray(B, n);
randArray(B, n, -10, 10);
wasInput = true;
break;
}
case 2: {
if (wasInput)
print(B, n);
else printf("Please, input array\n");
break;
}
case 3: {
if (wasInput)
{
BubbleSort(B, n);
print(B, n);
}
else printf("Please, input array\n");
break;
}
//default: printf("error\n");
// break;
}
}
//randArray(B, n, -10, 10);
//print(B, n);
}