-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCombSort.cs
More file actions
39 lines (33 loc) · 1.06 KB
/
CombSort.cs
File metadata and controls
39 lines (33 loc) · 1.06 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
using System;
namespace Algorithms.Sort
{
public class CombSort : IAlgorithm
{
private int[] data = new int[12] { 7, 12, 3, 9, 4, 56, 9, 0, 2, 5, 1, 2 };
public void Run()
{
var distance = data.Length - 1;
Console.WriteLine(string.Join(",", data));
do
{
var swapCount = 0;
for (int startIdx = 0; startIdx + distance < data.Length; startIdx++)
{
if (data[startIdx] > data[startIdx + distance])
{
var tmp = data[startIdx + distance];
data[startIdx + distance] = data[startIdx];
data[startIdx] = tmp;
swapCount++;
}
}
distance--;
if (distance < 1 && swapCount != 0)
{
distance++;
}
} while (distance >= 1);
Console.WriteLine(string.Join(",", data));
}
}
}