-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShellSort.cs
More file actions
36 lines (30 loc) · 958 Bytes
/
ShellSort.cs
File metadata and controls
36 lines (30 loc) · 958 Bytes
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
using System;
namespace Algorithms.Sort
{
public class ShellSort : IAlgorithm
{
private int[] data = new int[12] { 7, 12, 3, 9, 4, 56, 9, 0, 2, 5, 1, 2 };
public void Run()
{
Console.WriteLine(string.Join(",", data));
int step = data.Length - 1;
while (step > 0)
{
for (int i = step; i < data.Length; i ++)
{
int swpIdx = i;
while (data[swpIdx] < data[swpIdx - step])
{
var buf = data[swpIdx - step];
data[swpIdx - step] = data[swpIdx];
data[swpIdx] = buf;
swpIdx -= step;
if(swpIdx - step < 0) break;
}
}
step >>= 1;
}
Console.WriteLine(string.Join(",", data));
}
}
}