-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
267 lines (264 loc) · 8.54 KB
/
Program.cs
File metadata and controls
267 lines (264 loc) · 8.54 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
using System;
namespace Tuan2
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("=======================================================");
Console.Write("Nhap so ban muon tim: ");
int n = int.Parse(Console.ReadLine());
Console.Write("Ban muon random so lon nhat la bao nhieu?: ");
int max = int.Parse(Console.ReadLine());
Console.Write("Ban muon co bao nhieu phan tu trong mang?: ");
int len = int.Parse(Console.ReadLine());
MyIntArray a = new MyIntArray(len);
a.RandomArray(max);
a.OutputArray();
if (a.FindContent(n) == -1)
{
Console.WriteLine("\nKhong tim thay gia tri can tim!");
}
else
{
Console.WriteLine($"\nTim thay {n} o vi tri i = {a.FindContent(n)} (Xuat phat i = 0)");
}
if(a.CheckArray() == "Increase"){
Console.WriteLine("Mang tang dan nen ta co the tim kiem tuan tu hay nhi phan deu duoc");
}
else if(a.CheckArray() == "Decrease")
{
Console.WriteLine("Mang giam dan nen ta chi co the tim kiem tuan tu");
}
else if(a.CheckArray() == "Not_in_order")
{
Console.WriteLine("Mang khong co thu tu nen ta chi co the tim kiem tuan tu");
}
Console.WriteLine($"So lan thuc hien so sanh tuan tu la: {a.CountLinearSearchSteps(n)}");
Array.Sort(a.Mang);
Console.WriteLine($"So lan thuc hien so sanh nhi phan (sau khi duoc sap xep) la: {a.CountBinarySearchSteps(n)}");
Console.WriteLine("=======================================================");
}
}
class MyIntArray
{
private int[] array;
public int[] Mang
{
get => this.array;
set { this.array = value; }
}
public MyIntArray(int n = 8)
{
Mang = new int[n];
}
public int this[int i]
{
get => array[i];
set => array[i] = value;
}
public void Input()
{
String[] tk = Console.ReadLine().Split();
for(int i = 0; i< Mang.Length;i++)
{
Mang[i] = int.Parse(tk[i]);
}
}
public void RandomArray(int max) //phát sinh ngẫu nhiên mảng
{
for (int i = 0; i < Mang.Length; i++)
{
Random x = new Random();
Mang[i] = x.Next(max);
}
}
public void OutputArray() //Xuất mảng
{
Console.WriteLine("KET QUA MANG:");
for (int i = 0; i < Mang.Length; i++)
{
Console.Write(Mang[i] + " ");
}
}
public int FindContent(int x) //Tìm kiếm theo phương pháp tuần tự
{
for (int i = 0; i < Mang.Length; i++)
{
if (Mang[i] == x)
{
return i;
}
}
return -1;
}
public int CountLinearSearchSteps(int number_being_find) //Đếm số lần thực hiện tìm kiếm theo phương pháp tuần tự
{
for (int i = 0; i < Mang.Length; i++)
{
if (Mang[i] == number_being_find)
{
return i + 1;
}
}
return Mang.Length;
}
public int CountBinarySearchSteps(int number_being_find) //Đếm số lần thực hiện tìm kiếm theo phương pháp nhị phân
{
int left = 0;
int right = Mang.Length - 1;
int mid;
int found = 0;
while (left <= right)
{
mid = (left + right) / 2;
found++;
if (Mang[mid] == number_being_find)
{
break;
}
else if (number_being_find < Mang[mid])
{
right = mid - 1;
}
else if (number_being_find > Mang[mid])
{
left = mid + 1;
}
}
return found;
}
public string CheckArray() //Kiểm tra xem mảng tăng dần, giảm dần hay không có thứ tự
{
int count_increase = 0;
int count_decrease = 0;
for(int i = 0; i< Mang.Length - 1; i++)
{
if(Mang[i+1]>=Mang[i])
{
count_increase++;
}
if(Mang[i+1]<=Mang[i])
{
count_decrease++;
}
}
if(count_increase == Mang.Length - 1)
{
return "Increase";
}
else if(count_decrease == Mang.Length - 1)
{
return "Decrease";
}
return "Not_in_order";
}
public void Swap(ref int Mang,ref int b) //Phương thức đổi chỗ 2 số
{
int temp = Mang;
Mang = b;
b = temp;
}
public void BubbleSort() //Sắp xếp nổi bọt
{
for (int i = 0; i < Mang.Length - 1; i++)
{
bool change = false; //cải tiến
for (int j = Mang.Length - 1; j > i; j--)
{
if (Mang[j - 1] > Mang[j])
{
Swap(ref Mang[j],ref Mang[j-1]);
change = true;
}
}
if (change == false) //cải tiến
{
break;
}
}
Console.WriteLine("This array has been sorted by BubbleSort!");
}
public void InterchangeSort()
{
for (int i = 0; i < Mang.Length - 1; i++)
{
bool change = false;
for (int j = i + 1; j < Mang.Length; j++)
{
if (Mang[i] > Mang[j])
{
Swap(ref Mang[i],ref Mang[j]);
change = true;
}
}
if(change == false)
{
break;
}
}
Console.WriteLine("This array has been sorted by InterchangeSort!");
}
public void SelectionSort() //Sắp xếp kiểu tìm đứa nhỏ nhất nhét lên đầu
{
for(int i = 0; i<Mang.Length - 1;i++)
{
int min = i;
for(int j = i + 1;j<Mang.Length;j++)
{
if(Mang[j] < Mang[min])
{
min = j;
}
}
if(min!= i)
{
Swap(ref Mang[min], ref Mang[i]);
}
}
Console.WriteLine("Array has been sorted by SelectionSort!");
}
public void InsertionSort() //Sắp xếp kiểu chèn
{
for(int i = 1; i<Mang.Length;i++)
{
int x = Mang[i];
bool flag = false;
for(int j = i - 1;j>=0 && flag != true;)
{
if(x<Mang[j])
{
Mang[j + 1] = Mang[j];
j--;
Mang[j + 1] = x;
}
else
{
flag = true;
}
}
}
Console.WriteLine("Array has sorted by InsertionSort!");
}
public void QuickSort(int[] a, int left, int right) //Sắp xếp nhanh
{
int pivot, i,j;
i = left;
j = right;
pivot = a[(left+right)/2];
while(i<j)
{
while(a[i] < pivot) i++;
while(a[j] > pivot) j--;
if(i<=j)
{
Swap(ref a[i], ref a[j]);
i++;
j--;
}
}
if(left < j) QuickSort(a,left,j);
if(right > i) QuickSort(a,i,right);
}
}
}