forked from sylviot/algoritmos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShell.cs
49 lines (41 loc) · 742 Bytes
/
Shell.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
using System;
public class ShellSort
{
public static void Main()
{
int[] array = new int[]{3,1,2,5,4};
int[] result = Shell(array);
foreach(var item in result){
Console.WriteLine(item);
}
}
public static int[] Shell(int[] array)
{
bool flag = false;
int d = array.Length;
while(flag || (d > 1))
{
flag = false;
d = (d + 1) / 2;
for(int i = 0; i < (array.Length - d); i++)
{
if(NeedSwap(array[i+d], array[i]))
{
Swap(array, i + d, i);
flag = true;
}
}
}
return array;
}
public static bool NeedSwap(int a, int b)
{
return a > b;
}
public static void Swap(int[] array, int a, int b)
{
int aux = array[a];
array[a] = array[b];
array[b] = aux;
}
}