using System.Collections.Generic;
public static void Main()
int[] blades = { 500, 530, 550, 475, 450, 525, 460, 505 };
foreach (int blade in SortBlades(blades)) Console.WriteLine(blade);
public static int[] SortBlades(int[] blades)
blades = blades.OrderByDescending(b => b).ToArray();
int[] result = new int[blades.Length];
if (result.Length == 0) return new int[0];
for (int i = 1; i < blades.Length - 1; i++)
var sequences = new List<Sequence>();
int tmpStartIndex = 0, tmpEndIndex = 0;
for (int j = 1; j < result.Length; j++)
if (result[j] == 0 && result[j-1] != 0) tmpStartIndex = j;
if (result[j] != 0 && result[j-1] == 0) tmpEndIndex = j - 1;
else if (result[j] == 0 && j == result.Length - 1) tmpEndIndex = j;
if (tmpEndIndex != 0) sequences.Add(new Sequence() { StartIndex = tmpStartIndex, EndIndex = tmpEndIndex });
foreach (var sequence in sequences) sequence.SumOfWeightsAround = result[sequence.StartIndex - 1] + (sequence.EndIndex == result.Length - 1 ? result[0] : result[sequence.EndIndex + 1]);
result[sequences.OrderByDescending(s => s.EndIndex - s.StartIndex).ThenBy(s => s.SumOfWeightsAround).Select(s => (s.StartIndex + s.EndIndex) / 2).First()] = blades[i];
result[result.Select((v, i) => new { Index = i, Value = v }).Where(e => e.Value == 0).Select(e => e.Index).First()] = blades[blades.Length - 1];
public int StartIndex { get; set; }
public int EndIndex { get; set; }
public int SumOfWeightsAround { get; set; }