Median)
1. Definition: a group of data is arranged in sequence from small to large (or from large to small), and the number (or the average of the two data in the center). Note: the median is not necessarily in this set of data ).
Note: When the number is the base, the number at the center is used. When the number is an even number, the average of the two numbers in the middle is used.
2. Sort from small to large. You can first sort by bubble and then take the median. First, let's look at the Bubble sorting algorithm. The Code is as follows:
public static void BubbleSort(this IList<double> array) { if (array == null || array.Count == 0) { return; } int count = array.Count; for (int i = 0; i < count - 1; i++) { for (int j = 0; j < count - i - 1; j++) { if (array[j + 1] < array[j]) { double temp = array[j + 1]; array[j + 1] = array[j]; array[j] = temp; } } } } public static void BubbleSort<T>(this IList<T> array) where T : IComparable { if (array == null || array.Count == 0) { return; } int count = array.Count; for (int i = 0; i < count - 1; i++) { for (int j = 0; j < count - i - 1; j++) { if (array[j + 1].CompareTo(array[j]) < 0) { T temp = array[j + 1]; array[j + 1] = array[j]; array[j] = temp; } } } }
One is a basic algorithm and the other is generic. Obtain the median value after sorting.
3. But we don't need to sort all the data from small to large. We only need to sort a part of the data (that is, improve the data on the basis of bubble). Let's look at the code first:
public static double Median(this IList<double> array) { if (array == null) { throw new ArgumentNullException("array"); } return array.ToArray().Median(); } public static double Median(this double[] array) { if (array == null) { throw new ArgumentNullException("array"); } int endIndex = array.Length / 2; for (int i = 0; i <= endIndex; i++) { for (int j = 0; j < array.Length - i - 1; j++) { if (array[j + 1] < array[j]) { double temp = array[j + 1]; array[j + 1] = array[j]; array[j] = temp; } } } if (array.Length % 2 != 0) { return array[endIndex]; } return (array[endIndex - 1] + array[endIndex]) / 2; } public static decimal Median(this IList<decimal> array) { if (array == null) { throw new ArgumentNullException("array"); } return array.ToArray().Median(); } public static decimal Median(this decimal[] array) { if (array == null) { throw new ArgumentNullException("array"); } int endIndex = array.Length / 2; for (int i = 0; i <= endIndex; i++) { for (int j = 0; j < array.Length - i - 1; j++) { if (array[j + 1] < array[j]) { decimal temp = array[j + 1]; array[j + 1] = array[j]; array[j] = temp; } } } if (array.Length % 2 != 0) { return array[endIndex]; } return (array[endIndex - 1] + array[endIndex]) / 2; }
Only two lines of improved code are available.
Int endIndex = array. Length/2;
For (int I = 0; I <= endIndex; I ++)
In half. I personally prefer to use decimal for computation. The error is small, although the execution speed is a little slower than other basic types.
4. The procedure is as follows:
Double [] array = {47,5, 6,-88,6, 1,-7,-17,-232 };
Double result = array. Median ();