Algorithm-median Calculation

Source: Internet
Author: User

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 ();

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.