NOrderStat.cs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. namespace AdditionalMathf
  5. {
  6. public static class NOrderStatExtension
  7. {
  8. /// <summary>
  9. /// Partitions the given list around a pivot element such that all elements on left of pivot are <= pivot
  10. /// and the ones at thr right are > pivot. This method can be used for sorting, N-order statistics such as
  11. /// as median finding algorithms.
  12. /// Pivot is selected ranodmly if random number generator is supplied else its selected as last element in the list.
  13. /// Reference: Introduction to Algorithms 3rd Edition, Corman et al, pp 171
  14. /// </summary>
  15. private static int Partition<T>(this IList<T> list, int start, int end, Random rnd = null)
  16. where T : IComparable<T>
  17. {
  18. if (rnd != null)
  19. list.Swap(end, rnd.Next(start, end + 1));
  20. var pivot = list[end];
  21. var lastLow = start - 1;
  22. for (var i = start; i < end; i++)
  23. if (list[i].CompareTo(pivot) <= 0)
  24. list.Swap(i, ++lastLow);
  25. list.Swap(end, ++lastLow);
  26. return lastLow;
  27. }
  28. /// <summary>
  29. /// Returns Nth smallest element from the list. Here n starts from 0 so that n=0 returns minimum, n=1 returns 2nd
  30. /// smallest element etc.
  31. /// Note: specified list would be mutated in the process.
  32. /// Reference: Introduction to Algorithms 3rd Edition, Corman et al, pp 216
  33. /// </summary>
  34. public static T NthOrderStatistic<T>(this IList<T> list, int n, Random rnd = null) where T : IComparable<T>
  35. {
  36. return NthOrderStatistic(list, n, 0, list.Count - 1, rnd);
  37. }
  38. private static T NthOrderStatistic<T>(this IList<T> list, int n, int start, int end, Random rnd)
  39. where T : IComparable<T>
  40. {
  41. while (true)
  42. {
  43. var pivotIndex = list.Partition(start, end, rnd);
  44. if (pivotIndex == n)
  45. return list[pivotIndex];
  46. if (n < pivotIndex)
  47. end = pivotIndex - 1;
  48. else
  49. start = pivotIndex + 1;
  50. }
  51. }
  52. public static void Swap<T>(this IList<T> list, int i, int j)
  53. {
  54. if (i == j) //This check is not required but Partition function may make many calls so its for perf reason
  55. return;
  56. var temp = list[i];
  57. list[i] = list[j];
  58. list[j] = temp;
  59. }
  60. /// <summary>
  61. /// Note: specified list would be mutated in the process.
  62. /// </summary>
  63. public static T Median<T>(this IList<T> list) where T : IComparable<T>
  64. {
  65. return list.NthOrderStatistic((list.Count - 1) / 2);
  66. }
  67. public static double Median<T>(this IEnumerable<T> sequence, Func<T, double> getValue)
  68. {
  69. var list = sequence.Select(getValue).ToList();
  70. var mid = (list.Count - 1) / 2;
  71. return list.NthOrderStatistic(mid);
  72. }
  73. }
  74. }