-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearchAlgorithms.cs
More file actions
256 lines (208 loc) · 6.64 KB
/
SearchAlgorithms.cs
File metadata and controls
256 lines (208 loc) · 6.64 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
using UnityEngine;
using System.Collections;
public class SearchAlgorithms : MonoBehaviour
{
#region Search
public void Search(Master.SearchAlgorithm algorithm)
{
Master.StopAllCoroutines();
if (algorithm==Master.SearchAlgorithm.Linear)
StartCoroutine("LinearSearch");
else if (algorithm==Master.SearchAlgorithm.Binary)
StartCoroutine("BinarySearch");
else if (algorithm==Master.SearchAlgorithm.Jump)
StartCoroutine("JumpSearch");
else if (algorithm==Master.SearchAlgorithm.Interpolation)
StartCoroutine("InterpolationSearch");
else if (algorithm==Master.SearchAlgorithm.Exponential)
StartCoroutine("ExponentialSearch");
else if (algorithm==Master.SearchAlgorithm.Ternary)
StartCoroutine("TernarySearch");
}
#endregion
#region Linear Search
IEnumerator LinearSearch()
{
int key = ArrayManager.SearchingNumber;
int i=0;
foreach(int num in ArrayManager.Array())
{
if(num == key)
{
ArrayManager.ChangeColorOfNumber(i, "yellow");
break;
}
ArrayManager.ChangeColorOfNumber(i, "red");
i++;
if(!Master.Instant)
yield return new WaitForSeconds(Master.StepLength);
}
}
#endregion
#region Binary Search
IEnumerator BinarySearch()
{
var arr = ArrayManager.Array();
int left = 0;
int right = arr.Length-1;
int key = ArrayManager.SearchingNumber;
while(left <= right)
{
int mid = (left+right) / 2;
int midVal = arr[mid];
if(midVal == key)
{
ArrayManager.ChangeColorOfNumber(mid, "yellow");
break;
}
else if(midVal > key) right = mid-1;
else if(midVal < key) left = mid+1;
ArrayManager.ChangeColorOfNumber(mid, "red");
if(!Master.Instant)
yield return new WaitForSeconds(Master.StepLength);
}
}
#endregion
#region Jump Search
IEnumerator JumpSearch()
{
var arr = ArrayManager.Array();
int len = arr.Length;
int lengthRoot = Mathf.FloorToInt(Mathf.Sqrt(len));
int step = lengthRoot;
int prev = 0;
int key = ArrayManager.SearchingNumber;
while(arr[Mathf.Min(step,len) - 1] < key)
{
prev = step;
step += lengthRoot;
if(prev >= len)
break;
if(!Master.Instant)
{
ArrayManager.ChangeColorOfNumber(prev, "red");
yield return new WaitForSeconds(Master.StepLength);
}
}
while(arr[prev] < key)
{
prev++;
if(prev == Mathf.Min(step, len))
break;
if(!Master.Instant)
{
ArrayManager.ChangeColorOfNumber(prev, "red");
yield return new WaitForSeconds(Master.StepLength);
}
}
if(arr[prev] == key)
ArrayManager.ChangeColorOfNumber(prev, "yellow");
}
#endregion
#region Interpolation Search
IEnumerator InterpolationSearch()
{
var array = ArrayManager.Array();
int low = 0;
int high = (array.Length-1);
int key = ArrayManager.SearchingNumber;
while(low<=high && key>=array[low] && key<=array[high])
{
if (low == high)
{
if (array[low] == key)
ArrayManager.ChangeColorOfNumber(low, "yellow");
break;
}
int pos = low + (((high-low) / (array[high]-array[low]))) * (key-array[low]);
if(array[pos] == key)
{
ArrayManager.ChangeColorOfNumber(pos, "yellow");
break;
}
if(array[pos]<key) low = (pos+1);
else high = (pos-1);
ArrayManager.ChangeColorOfNumber(pos, "red");
if(!Master.Instant)
yield return new WaitForSeconds(Master.StepLength);
}
}
#endregion
#region Exponential Search
IEnumerator ExponentialSearch()
{
var arr = ArrayManager.Array();
int key = ArrayManager.SearchingNumber;
int len = arr.Length;
bool found = false;
if(arr[0] == key)
{
ArrayManager.ChangeColorOfNumber(0, "red");
found = true;
}
int i=1;
while((i<len) && (arr[i]<=key) && !found)
{
i *= 2;
ArrayManager.ChangeColorOfNumber(i, "red");
if(!Master.Instant)
yield return new WaitForSeconds(Master.StepLength);
}
// BINARY SEARCH
int result = 0;
int left = 0;
int right = arr.Length-1;
while(left <= right)
{
int mid = (left+right) / 2;
if(arr[mid] == key)
{
result = mid;
break;
}
else if(arr[mid] < key) left = mid+1;
else if(arr[mid] > key) right = mid-1;
ArrayManager.ChangeColorOfNumber(mid, "red");
if(!Master.Instant)
yield return new WaitForSeconds(Master.StepLength);
}
// #BINARY SEARCH
ArrayManager.ChangeColorOfNumber(result, "yellow");
}
#endregion
#region Ternary Search
IEnumerator TernarySearch()
{
var arr = ArrayManager.Array();
int key = ArrayManager.SearchingNumber;
int l = 0;
int r = arr.Length;
while (r >= l)
{
int mid1 = l + (r - l) / 3;
int mid2 = r - (r - l) / 3;
if (arr[mid1] == key)
{
ArrayManager.ChangeColorOfNumber(mid1, "yellow");
break;
}
else if (arr[mid2] == key)
{
ArrayManager.ChangeColorOfNumber(mid2, "yellow");
break;
}
if (key < arr[mid1]) r = mid1-1;
else if (key > arr[mid2]) l = mid2+1;
else {
l = mid1+1;
r = mid2-1;
}
if(!Master.Instant)
{
ArrayManager.ChangeColorOfTwoNumbers(mid1, mid2, "red");
yield return new WaitForSeconds(Master.StepLength);
}
}
}
#endregion
}