-
Notifications
You must be signed in to change notification settings - Fork 0
/
SortAlgorithms.cs
184 lines (150 loc) · 4.16 KB
/
SortAlgorithms.cs
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
using UnityEngine;
using System.Collections;
public class SortAlgorithms : MonoBehaviour
{
#region Sort
public void Sort(Master.SortAlgorithm algorithm)
{
Master.StopAllCoroutines();
if (algorithm==Master.SortAlgorithm.Selection)
StartCoroutine("SelectionSort");
else if (algorithm==Master.SortAlgorithm.Bubble)
StartCoroutine("BubbleSort");
else if (algorithm==Master.SortAlgorithm.Insertion)
StartCoroutine("InsertionSort");
else if (algorithm==Master.SortAlgorithm.Merge)
StartCoroutine("MergeSort");
}
#endregion
#region Selection Sort
IEnumerator SelectionSort()
{
var arr = ArrayManager.Array();
int len = arr.Length;
for(int i=0; i<len-1; i++)
{
int min = i;
for(int j=i+1; j<len; j++)
{
if(arr[j] < arr[min])
min = j;
}
bool swapped = arr[min] != arr[i];
int toSwap = arr[min];
arr[min] = arr[i];
arr[i] = toSwap;
if(swapped && !Master.Instant)
{
ArrayManager.ChangeColorOfNumber(i, "red");
yield return new WaitForSeconds(Master.StepLength);
}
}
ArrayManager.ResetUI();
}
#endregion
#region Bubble Sort
IEnumerator BubbleSort()
{
var arr = ArrayManager.Array();
int len = arr.Length;
for(int x=0; x<len-1; x++){
bool swapped = false;
for(int y=0; y<len-x-1; y++){
if(arr[y] > arr[y+1])
{
swapped = (arr[y] != arr[y+1]);
int toSwap = arr[y];
arr[y] = arr[y+1];
arr[y+1] = toSwap;
}
}
if(swapped && !Master.Instant)
{
ArrayManager.ChangeColorOfNumber(x, "red");
yield return new WaitForSeconds(Master.StepLength);
}
}
ArrayManager.ResetUI();
}
#endregion
#region Insertion Sort
IEnumerator InsertionSort()
{
var array = ArrayManager.Array();
int len = array.Length;
for(int i=1; i<len; i++)
{
int key = array[i];
int j = i-1;
while(j>=0 && array[j]>key)
{
array[j+1] = array[j];
j--;
if(!Master.Instant)
{
ArrayManager.ChangeColorOfNumber(j+1, "red");
yield return new WaitForSeconds(Master.StepLength);
}
}
array[j+1] = key;
}
ArrayManager.ResetUI();
}
#endregion
#region Merge Sort
IEnumerator MergeSort()
{
var len = ArrayManager.Array().Length;
Sort(0, len-1);
ArrayManager.ResetUI();
yield return null;
}
void Sort(int left, int right)
{
if(left < right)
{
int mid = (left+right) / 2;
Sort(left, mid);
Sort(mid+1, right);
Merge(left, mid, right);
}
}
void Merge(int left, int mid, int right)
{
var array = ArrayManager.Array();
var L = new int[(mid - left + 1)];
var R = new int[(right - mid)];
for(int x=0; x<L.Length; x++) L[x] = array[left + x];
for(int x=0; x<R.Length; x++) R[x] = array[mid + x + 1];
int iLeft = 0;
int iRight = 0;
int k = left;
while(iLeft<L.Length && iRight<R.Length)
{
if(L[iLeft] <= R[iRight])
{
array[k] = L[iLeft];
iLeft++;
}
else
{
array[k] = R[iRight];
iRight++;
}
k++;
}
while(iLeft<L.Length)
{
array[k] = L[iLeft];
iLeft++;
k++;
}
while(iRight<R.Length)
{
array[k] = R[iRight];
iRight++;
k++;
}
}
#endregion
}