Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static class MassiveExtends
- {
- public enum GetType
- {
- Max,
- Min
- }
- public static int GetValue(int[] mas, GetType getType)
- {
- if (mas.Length > 0)
- {
- int searchValue = mas[0];
- for (int i = 0; i < mas.Length; i++)
- {
- if ((getType == GetType.Max && mas[i] > searchValue) ||
- (getType == GetType.Min && mas[i] < searchValue))
- {
- searchValue = mas[i];
- }
- }
- return searchValue;
- }
- return -1;
- }
- public static bool Contains(int[] a, int value)
- {
- int first = 0;
- int last = a.Length - 1;
- int middle = first + last / 2;
- while (last - first > 1)
- {
- if (a[middle] < value)
- first = middle;
- else
- last = middle;
- middle = first + last / 2;
- }
- if (a[middle] == value)
- return true;
- else
- return false;
- }
- public static int[] SortSelect(int[] a)
- {
- for (int i = 0; i < a.Length; i++)
- {
- int min = i;
- for (int j = i + 1; j < a.Length; j++)
- {
- if (a[j] < a[min])
- {
- min = j;
- }
- }
- Swap(ref a[min], ref a[i]);
- }
- return a;
- }
- public static int[] InsertionSort(int[] array)
- {
- for (var i = 0; i < array.Length; i++)
- {
- var key = array[i];
- var j = i;
- while ((j > 0) && (array[j - 1] > key))
- {
- Swap(ref array[j - 1], ref array[j]);
- j--;
- }
- array[j] = key;
- }
- return array;
- }
- public static int[] BubbleSort(int[] array)
- {
- for (int i = 0; i < array.Length; i++)
- {
- for (int j = i + 1; j < array.Length; j++)
- {
- if (array[i] > array[j])
- {
- Swap(ref array[i], ref array[j]);
- }
- }
- }
- return array;
- }
- static void Swap(ref int e1, ref int e2)
- {
- var temp = e1;
- e1 = e2;
- e2 = temp;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement