static double GetDistanceIndex(int value, List<int> others) { double result=0; foreach (var other in others) { result += Math.Sqrt(Math.Abs(other - value)); } return result; } static List<int> Sort(List<int> items, int initialValue) { items = items.ToList(); List<int> result=new List<int>(); lock (rnd) { while (true) { result.Add(initialValue); items.Remove(initialValue); if (items.Count==0Count == 0) { break; } Dictionary<int Dictionary<double, List<int>> distances = new Dictionary<intDictionary<double, List<int>>(); foreach (var item in items) { var d = GetDistanceIndex(item, result); if (!distances.ContainsKey(d)) { distances[d] = new List<int>(); } distances[d].Add(item); } var max = distances.Keys.Max(); var l = distances[max]; //if (l.Count == 1) //{ // initialValue = l[0]; //} //else //{ initialValue = l[Rndl[rnd.Next(l.Count)]; //} } } return result; } static double GetDistanceIndex(int value, List<int> others) { double result=0; foreach (var other in others) { result += Math.Sqrt(Math.Abs(other - value)); } return result; } static List<int> Sort(List<int> items, int initialValue) { items = items.ToList(); List<int> result=new List<int>(); while (true) { result.Add(initialValue); items.Remove(initialValue); if (items.Count==0) { break; } Dictionary<int, List<int>> distances = new Dictionary<int, List<int>>(); foreach (var item in items) { var d = GetDistanceIndex(item, result); if (!distances.ContainsKey(d)) { distances[d] = new List<int>(); } distances[d].Add(item); } var max = distances.Keys.Max(); var l = distances[max]; if (l.Count == 1) { initialValue = l[0]; } else { initialValue = l[Rnd.Next(l.Count)]; } } return result; } static double GetDistanceIndex(int value, List<int> others) { double result=0; foreach (var other in others) { result += Math.Sqrt(Math.Abs(other - value)); } return result; } static List<int> Sort(List<int> items, int initialValue) { items = items.ToList(); List<int> result=new List<int>(); lock (rnd) { while (true) { result.Add(initialValue); items.Remove(initialValue); if (items.Count == 0) { break; } Dictionary<double, List<int>> distances = new Dictionary<double, List<int>>(); foreach (var item in items) { var d = GetDistanceIndex(item, result); if (!distances.ContainsKey(d)) { distances[d] = new List<int>(); } distances[d].Add(item); } var max = distances.Keys.Max(); var l = distances[max]; //if (l.Count == 1) //{ // initialValue = l[0]; //} //else //{ initialValue = l[rnd.Next(l.Count)]; //} } } return result; } static intdouble GetDistanceIndex(int value, List<int> others) { intdouble result=0; foreach (var other in others) { result += Math.Sqrt(Math.Abs(other - value)); } return result; } static List<int> Sort(List<int> items, int initialValue) { items = items.ToList(); List<int> result=new List<int>(); while (true) { result.Add(initialValue); items.Remove(initialValue); if (items.Count==0) { break; } Dictionary<int, List<int>> distances = new Dictionary<int, List<int>>(); foreach (var item in items) { var d = GetDistanceIndex(item, result); if (!distances.ContainsKey(d)) { distances[d] = new List<int>(); } distances[d].Add(item); } var max = distances.Keys.Max(); var l = distances[max]; if (l.Count == 1) { initialValue = l[0]; } else { initialValue = l[Rnd.Next(l.Count)]; } } return result; } I'm using this algorithm to select numbers as different from each other as possible from a fixed number of alternatives
UPDATE 2
Dukeling in his answer pointed out to me, that {1,9,2,8,3,7,4,6,5} also conforms to my requirements. This was true, and it's my mistake. I want the numbers to be as far spaced as possible, and 3d number being very close to the first one is not what I intended. So I'm updating the distance function to reflect this
static int GetDistanceIndex(int value, List<int> others) { int result=0; foreach (var other in others) { result += Math.Abs(other - value); } return result; } static List<int> Sort(List<int> items, int initialValue) { items = items.ToList(); List<int> result=new List<int>(); while (true) { result.Add(initialValue); items.Remove(initialValue); if (items.Count==0) { break; } Dictionary<int, List<int>> distances = new Dictionary<int, List<int>>(); foreach (var item in items) { var d = GetDistanceIndex(item, result); if (!distances.ContainsKey(d)) { distances[d] = new List<int>(); } distances[d].Add(item); } var max = distances.Keys.Max(); var l = distances[max]; if (l.Count == 1) { initialValue = l[0]; } else { initialValue = l[Rnd.Next(l.Count)]; } } return result; } I'm using this algorithm to select numbers as different from each other as possible from a fixed number of alternatives
static double GetDistanceIndex(int value, List<int> others) { double result=0; foreach (var other in others) { result += Math.Sqrt(Math.Abs(other - value)); } return result; } static List<int> Sort(List<int> items, int initialValue) { items = items.ToList(); List<int> result=new List<int>(); while (true) { result.Add(initialValue); items.Remove(initialValue); if (items.Count==0) { break; } Dictionary<int, List<int>> distances = new Dictionary<int, List<int>>(); foreach (var item in items) { var d = GetDistanceIndex(item, result); if (!distances.ContainsKey(d)) { distances[d] = new List<int>(); } distances[d].Add(item); } var max = distances.Keys.Max(); var l = distances[max]; if (l.Count == 1) { initialValue = l[0]; } else { initialValue = l[Rnd.Next(l.Count)]; } } return result; } I'm using this algorithm to select numbers as different from each other as possible from a fixed number of alternatives
UPDATE 2
Dukeling in his answer pointed out to me, that {1,9,2,8,3,7,4,6,5} also conforms to my requirements. This was true, and it's my mistake. I want the numbers to be as far spaced as possible, and 3d number being very close to the first one is not what I intended. So I'm updating the distance function to reflect this
- initial seed : 1. Really, it can be any number. We can select 5, and get something like {5,9,1,2,8,3,7,6,4}
- From the provided array, 9 is the farthest away from 1 by distances
- Because in the list all the numbers are equidistant from 1 and from 9, we can select any of them. I used rand to select 6.
- Now we are looking for a number farthest away from {1,9,6}. 2 is selected because
abs(2-1)+abs(2-9)+abs(2-6)=12and is greater thanabs(3-1)+abs(3-9)+abs(3-6)=11orabs(4-1)+abs(4-9)+abs(4-6)=10orabs(8-1)+abs(8-9)+abs(8-6)=10orabs(7-1)+abs(7-9)+abs(7-6)=9orabs(5-1)+abs(5-9)+abs(5-6)=9 - etc
- initial seed : 1. Really, it can be any number. We can select 5, and get something like {5,9,1,2,8,3,7,6,4}
- From the provided array, 9 is the farthest away from 1 by distances
- Because in the list all the numbers are equidistant from 1 and from 9, we can select any of them. I used rand to select 6.
- Now we are looking for a number farthest away from {1,9,6}
- etc
- initial seed : 1. Really, it can be any number. We can select 5, and get something like {5,9,1,2,8,3,7,6,4}
- From the provided array, 9 is the farthest away from 1 by distances
- Because in the list all the numbers are equidistant from 1 and from 9, we can select any of them. I used rand to select 6.
- Now we are looking for a number farthest away from {1,9,6}. 2 is selected because
abs(2-1)+abs(2-9)+abs(2-6)=12and is greater thanabs(3-1)+abs(3-9)+abs(3-6)=11orabs(4-1)+abs(4-9)+abs(4-6)=10orabs(8-1)+abs(8-9)+abs(8-6)=10orabs(7-1)+abs(7-9)+abs(7-6)=9orabs(5-1)+abs(5-9)+abs(5-6)=9 - etc