Advertisement
Guest User

Untitled

a guest
Jul 20th, 2019
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.75 KB | None | 0 0
  1. public static class  MassiveExtends
  2.     {
  3.         public enum GetType
  4.         {
  5.             Max,
  6.             Min
  7.         }
  8.  
  9.         public static int GetValue(int[] mas, GetType getType)
  10.         {
  11.             if (mas.Length > 0)
  12.             {
  13.                 int searchValue = mas[0];
  14.                
  15.                 for (int i = 0; i < mas.Length; i++)
  16.                 {
  17.                     if ((getType == GetType.Max && mas[i] > searchValue) ||
  18.                             (getType == GetType.Min && mas[i] < searchValue))
  19.                     {
  20.                         searchValue = mas[i];
  21.                     }
  22.                 }
  23.                 return searchValue;
  24.             }
  25.             return -1;
  26.         }
  27.  
  28.         public static bool Contains(int[] a, int value)
  29.         {
  30.             int first = 0;
  31.             int last = a.Length - 1;
  32.             int middle = first + last / 2;
  33.             while (last - first > 1)
  34.             {
  35.                 if (a[middle] < value)
  36.                     first = middle;
  37.                 else
  38.                     last = middle;
  39.                 middle = first + last / 2;
  40.             }
  41.             if (a[middle] == value)
  42.                 return true;
  43.             else
  44.                 return false;
  45.         }
  46.  
  47.         public static int[] SortSelect(int[] a)
  48.         {
  49.             for (int i = 0; i < a.Length; i++)
  50.             {
  51.                 int min = i;
  52.                 for (int j = i + 1; j < a.Length; j++)
  53.                 {
  54.                     if (a[j] < a[min])
  55.                     {
  56.                         min = j;
  57.                     }
  58.                 }
  59.                 Swap(ref a[min], ref a[i]);
  60.             }
  61.             return a;
  62.         }
  63.  
  64.         public static int[] InsertionSort(int[] array)
  65.         {
  66.             for (var i = 0; i < array.Length; i++)
  67.             {
  68.                 var key = array[i];
  69.                 var j = i;
  70.                 while ((j > 0) && (array[j - 1] > key))
  71.                 {
  72.                     Swap(ref array[j - 1], ref array[j]);
  73.                     j--;
  74.                 }
  75.                 array[j] = key;
  76.             }
  77.             return array;
  78.         }
  79.  
  80.         public static int[] BubbleSort(int[] array)
  81.         {
  82.             for (int i = 0; i < array.Length; i++)
  83.             {
  84.                 for (int j = i + 1; j < array.Length; j++)
  85.                 {
  86.                     if (array[i] > array[j])
  87.                     {
  88.                         Swap(ref array[i], ref array[j]);
  89.                     }
  90.                 }
  91.             }
  92.             return array;
  93.         }
  94.  
  95.         static void Swap(ref int e1, ref int e2)
  96.         {
  97.             var temp = e1;
  98.             e1 = e2;
  99.             e2 = temp;
  100.         }
  101.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement