public static int[] shellSort(int[] ar)
{
//this gaps array works but is not unique
int[] gaps = new int[] { 1, 4, 10, 23, 57, 132, 301, 701 };
gaps = gaps.Reverse().ToArray();
Func<int, int, bool> potentialSwitch = (ind1, ind2) =>
{
if (ar[ind2] < ar[ind1])
{
int temp = ar[ind2];
ar[ind2] = ar[ind1];
ar[ind1] = temp;
return true;
}
return false;
};
foreach (int gap in gaps)
{
if (gap >= ar.Length)
continue;
for (int i = 0; i + gap < ar.Length; i++)
{
int j = i;
while (potentialSwitch(j, j + gap))
{
j -= gap;
if (j < 0)
break;
}
}
}
return ar;
}