1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace GetMedian 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 int[] list = { 1,2,5,3,9,7,15,0,4}; 14 SortList(list); 15 foreach (int a in list) 16 { 17 Console.Write(a + ","); 18 } 19 double mid = GetMedian(list); 20 Console.WriteLine("The midian is {0}",mid); 21 Console.ReadKey(); 22 } 23 public static void SortList(int[] list) 24 { 25 for (int i = 0; i < list.Length; i++) 26 { 27 for (int j = 0; j < list.Length - 1 - i; j++) 28 { 29 if (list[j] > list[j + 1]) 30 { 31 int temp = list[j]; 32 list[j] = list[j + 1]; 33 list[j + 1] = temp; 34 } 35 } 36 } 37 } 38 public static double GetMedian(int[] list) 39 { 40 double mid = 0; 41 if (list.Length % 2 == 1) 42 { 43 mid = list[list.Length / 2 ]; 44 } 45 else 46 { 47 mid = (double)(list[list.Length / 2] + list[list.Length / 2 - 1]) / 2.0; 48 } 49 return mid; 50 } 51 } 52 }