-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShellSort.cs
44 lines (37 loc) · 957 Bytes
/
ShellSort.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
namespace Sort
{
public class ShellSort
{
public static string[] Sort(string[] arr)
{
var size = arr.Length;
var h = 1;
// find max h size for an array
while (h < (size / 3))
{
h = (3 * h + 1);
}
while (h > 0)
{
for(int i = h; i < size; i++)
{
for (int j = i; j >= h; j -= h)
{
if(arr[j].CompareTo(arr[j - h]) < 0)
{
Swap(arr, j, j-h);
}
}
}
h = h / 3;
}
return arr;
}
private static void Swap(string[] arr, int from, int to)
{
var value = arr[to];
arr[to] = arr[from];
arr[from] = value;
}
}
}