hozer

AISD - Lab2.3

Mar 29th, 2015
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.22 KB | None | 0 0
  1. using System;
  2.  
  3. namespace ConsoleApplication2
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             float[] arr= { 1, 300, 2, 200, 1 };
  10.             int k = 0, n;
  11.             n = arr.Length;
  12.             median(arr, n, ref k);
  13.             Console.WriteLine(arr[k]);
  14.             Console.ReadKey();
  15.         }
  16.  
  17.         private static void split(float[] a, int n, float x, ref int i, ref int j)
  18.         {
  19.             do
  20.             {
  21.                 while (a[i] < x) i++;
  22.                 while (x < a[j]) j--;
  23.  
  24.                 if (i <= j)
  25.                 {
  26.                     float t = a[i];
  27.                     a[i] = a[j];
  28.                     a[j] = t;
  29.                     i++; j--;
  30.                 }
  31.             } while (i <= j);
  32.         }
  33.     // O(2n)?
  34.         private static void median(float[] a, int n, ref int k)
  35.         {
  36.             int L = 0;
  37.             int R = n - 1;
  38.             k = n / 2;
  39.             int i; int j;
  40.             while (L < R)
  41.             {
  42.                 float x = a[k];
  43.                 i = L; j = R;
  44.                 split(a, n, x, ref i, ref j);
  45.                 if (j < k) L = i;
  46.                 if (k < i) R = j;
  47.             }
  48.         }
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment