NOrderStat.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. {
  24. if (list[i].CompareTo(pivot) <= 0)
  25. list.Swap(i, ++lastLow);
  26. }
  27. list.Swap(end, ++lastLow);
  28. return lastLow;
  29. }
  30. /// <summary>
  31. /// Returns Nth smallest element from the list. Here n starts from 0 so that n=0 returns minimum, n=1 returns 2nd smallest element etc.
  32. /// Note: specified list would be mutated in the process.
  33. /// Reference: Introduction to Algorithms 3rd Edition, Corman et al, pp 216
  34. /// </summary>
  35. public static T NthOrderStatistic<T>(this IList<T> list, int n, Random rnd = null) where T : IComparable<T>
  36. {
  37. return NthOrderStatistic(list, n, 0, list.Count - 1, rnd);
  38. }
  39. private static T NthOrderStatistic<T>(this IList<T> list, int n, int start, int end, Random rnd)
  40. where T : IComparable<T>
  41. {
  42. while (true)
  43. {
  44. var pivotIndex = list.Partition(start, end, rnd);
  45. if (pivotIndex == n)
  46. return list[pivotIndex];
  47. if (n < pivotIndex)
  48. end = pivotIndex - 1;
  49. else
  50. start = pivotIndex + 1;
  51. }
  52. }
  53. public static void Swap<T>(this IList<T> list, int i, int j)
  54. {
  55. if (i == j) //This check is not required but Partition function may make many calls so its for perf reason
  56. return;
  57. var temp = list[i];
  58. list[i] = list[j];
  59. list[j] = temp;
  60. }
  61. /// <summary>
  62. /// Note: specified list would be mutated in the process.
  63. /// </summary>
  64. public static T Median<T>(this IList<T> list) where T : IComparable<T>
  65. {
  66. return list.NthOrderStatistic((list.Count - 1) / 2);
  67. }
  68. public static double Median<T>(this IEnumerable<T> sequence, Func<T, double> getValue)
  69. {
  70. var list = sequence.Select(getValue).ToList();
  71. var mid = (list.Count - 1) / 2;
  72. return list.NthOrderStatistic(mid);
  73. }
  74. }
  75. }