Skip to main content
added 213 characters in body
Source Link
Arsen Zahray
  • 25.5k
  • 51
  • 143
  • 234
 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; } 
added 351 characters in body
Source Link
Arsen Zahray
  • 25.5k
  • 51
  • 143
  • 234
 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

added 248 characters in body
Source Link
Arsen Zahray
  • 25.5k
  • 51
  • 143
  • 234
  1. 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}
  2. From the provided array, 9 is the farthest away from 1 by distances
  3. 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.
  4. 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)=12 and is greater than abs(3-1)+abs(3-9)+abs(3-6)=11 or abs(4-1)+abs(4-9)+abs(4-6)=10 or abs(8-1)+abs(8-9)+abs(8-6)=10 or abs(7-1)+abs(7-9)+abs(7-6)=9 or abs(5-1)+abs(5-9)+abs(5-6)=9
  5. etc
  1. 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}
  2. From the provided array, 9 is the farthest away from 1 by distances
  3. 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.
  4. Now we are looking for a number farthest away from {1,9,6}
  5. etc
  1. 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}
  2. From the provided array, 9 is the farthest away from 1 by distances
  3. 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.
  4. 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)=12 and is greater than abs(3-1)+abs(3-9)+abs(3-6)=11 or abs(4-1)+abs(4-9)+abs(4-6)=10 or abs(8-1)+abs(8-9)+abs(8-6)=10 or abs(7-1)+abs(7-9)+abs(7-6)=9 or abs(5-1)+abs(5-9)+abs(5-6)=9
  5. etc
added 478 characters in body
Source Link
Arsen Zahray
  • 25.5k
  • 51
  • 143
  • 234
Loading
Source Link
Arsen Zahray
  • 25.5k
  • 51
  • 143
  • 234
Loading