using System.Collections.Generic;
public static void WriteArr <t>(t[] arr)
for (int i = 0; i < arr.Length; i++)
public static void Write2DArr<t>(t[,] arr)
for (int r = 0; r < arr.GetLength(0); r++)
for (int c = 0; c < arr.GetLength(1); c++)
Console.Write(arr[r, c]);
if (r != arr.GetLength(0) - 1 || c != arr.GetLength(1) - 1)
public static void FillArr(int[] arr, int num)
for (int i = 0; i < arr.Length; i++)
public static void SwapPosInArr <t>(t[] arr, int pos1, int pos2)
public static void Swap <t>(ref t num1, ref t num2)
public static bool IsSortedUp(int[] arr)
for (int i = 0; i < arr.Length - 1; i++)
public static bool IsSortedDown(int[] arr)
for (int i = 0; i < arr.Length - 1; i++)
public static void SelectionSort(int[] arr)
for (int i = 0; i < arr.Length; i++)
for (int j = i + 1; j < arr.Length; j++)
arr[smallestIndex] = arr[i];
public static void ReverseSelectionSort(int[] arr)
for (int i = 0; i < arr.Length; i++)
for (int j = i + 1; j < arr.Length; j++)
arr[biggestIndex] = arr[i];
private static int Partition(int[] arr)
int pivot = arr[arr.Length - 1];
int rightPointer = arr.Length-1;
while (leftPointer < rightPointer)
while (arr[leftPointer] <= pivot && leftPointer < rightPointer)
while (arr[rightPointer] >= pivot && leftPointer < rightPointer)
SwapPosInArr(arr, rightPointer, leftPointer);
SwapPosInArr(arr, leftPointer, arr.Length-1);
public static void QuickSort(int[] arr)
int tempLength = Partition(arr);
int[] leftArr = new int[tempLength];
int[] rightArr = new int[arr.Length - tempLength-1];
for (int i = 0; i < leftArr.Length; i++)
for (int i = 0; i < rightArr.Length; i++)
rightArr[i] = arr[num+1];
for (int i = 0; i < leftArr.Length; i++)
for (int i = 0; i < rightArr.Length; i++)
private static int ReturnMax(int[] arr)
for (int i = 1; i < arr.Length; i++)
public static void CountSort(int[] arr)
int[] countArr = new int[ReturnMax(arr) + 1];
for (int i = 0; i < countArr.Length; i++)
for (int i = 0; i < countArr.Length; i++)
for (int j = 0; j < arr.Length; j++)
for (int i = 0; i < countArr.Length; i++)
for (int j = 0; j < countArr[i]; j++)
public static void BogoSort(int[] arr)
Random rnd = new Random();
for (int i = 0; i < arr.Length; i++)
SwapPosInArr(arr, rnd.Next(0, arr.Length), rnd.Next(0, arr.Length));
public static int Factorial(int num)
return num * Factorial(num - 1);
public static void MergeSort (int[] arr)
int[] leftArr = new int[arr.Length / 2];
int[] rightArr = new int[arr.Length - leftArr.Length];
for (int i = 0; i < leftArr.Length; i++)
for (int i = 0; i < rightArr.Length; i++)
for (int i = 0; i < arr.Length; i++)
if (leftNum >= leftArr.Length)
arr[i] = rightArr[rightNum];
else if (rightNum >= rightArr.Length)
arr[i] = leftArr[leftNum];
else if (leftArr[leftNum] <= rightArr[rightNum])
arr[i] = leftArr[leftNum];
arr[i] = rightArr[rightNum];
public static int[] Merge(int[] leftArr, int[] rightArr)
int[] arr = new int[leftArr.Length + rightArr.Length];
for (int i = 0; i < arr.Length; i++)
if (leftNum >= leftArr.Length)
arr[i] = rightArr[rightNum];
else if (rightNum >= rightArr.Length)
arr[i] = leftArr[leftNum];
else if (leftArr[leftNum] <= rightArr[rightNum])
arr[i] = leftArr[leftNum];
arr[i] = rightArr[rightNum];
public static int[] TriMerge1(int[] arr1, int[] arr2, int[] arr3)
int[] newArr = Merge(arr1, arr2);
return Merge(newArr, arr3);
public static int[] TriMerge2(int[] arr1, int[] arr2, int[] arr3)
int num1 = 0, num2 = 0, num3 = 0;
int[] arr = new int[arr1.Length + arr2.Length + arr3.Length];
for (int i = 0; i < arr.Length; i++)
else if (num3 >= arr3.Length)
else if (arr2[num2] <= arr3[num3])
else if (num2 >= arr2.Length)
else if (num3 >= arr3.Length)
else if (arr1[num1] <= arr3[num3])
else if (arr1[num1] <= arr2[num2])
else if (arr1[num1] <= arr3[num3])
else if (arr2[num1] <= arr3[num3])
public static void WriteCommon(int[] arr1, int[] arr2)
int index1 = 0, index2 = 0;
while (index1 < arr1.Length && index2 < arr2.Length)
if (arr1[index1] == arr2[index2])
Console.WriteLine(arr2[index2]);
while (arr1.Length > index1 + 1 && flag)
if (arr1[index1 + 1] == arr1[index1])
while (arr2.Length > index2 + 1 && flag)
if (arr2[index2 + 1] == arr2[index2])
else if (arr1[index1] < arr2[index2])
public static int TriSearch(int[] arr, int num)
return TriSearch(arr, num, 0, arr.Length - 1);
private static int TriSearch(int[] arr, int num, int first, int last)
int third = (last - first) / 3;
int firstThird = third + first;
int secondThird = firstThird + third;
if (first == last && arr[firstThird] != num)
if (arr[firstThird] == num)
if (arr[firstThird] > num)
return TriSearch(arr, num, first, firstThird - 1);
if (arr[secondThird] >= num)
return TriSearch(arr, num, firstThird + 1, secondThird);
return TriSearch(arr, num, secondThird + 1, last);
public static int BinarySearch (int[] arr, int num)
return BinarySearch(arr, num, 0, arr.Length - 1);
private static int BinarySearch(int[] arr, int num, int first, int last)
int middle = (last - first)/2;
int middleIndex = first + middle;
if (first == last && arr[middleIndex] != num)
if (arr[middleIndex] == num)
if (arr[middleIndex] > num)
return BinarySearch(arr, num, first, middleIndex - 1);
return BinarySearch(arr, num, middleIndex + 1, last);
public static bool IsFirstStringBefore(string str1, string str2)
for (int i = 0; i < Math.Max(str1.Length, str2.Length); i++)
if (i == Math.Min(str1.Length, str2.Length))
else if (str1[i] > str2[i])
Console.WriteLine("Error");
public static int QuadSearch (int[] arr, int num)
return QuadSearch(arr, num, 0, arr.Length - 1);
private static int QuadSearch(int[] arr, int num, int first, int last)
int forth = (last - first) / 4;
int firstForth = forth + first;
int secondForth = firstForth + forth;
int thirdForth = secondForth + forth;
if (firstForth == last && arr[firstForth] != num)
if (arr[firstForth] == num)
if (arr[firstForth] > num)
return QuadSearch(arr, num, first, firstForth - 1);
if (arr[secondForth] >= num)
return QuadSearch(arr, num, firstForth + 1, secondForth);
if (arr[thirdForth] >= num)
return QuadSearch(arr, num, secondForth + 1, thirdForth);
return QuadSearch(arr, num, thirdForth + 1, last);
public static int[] Binary2DSearch(int[,] arr, int num)
int[] result = new int[2];
int firstCol = 0, firstRow = 0;
int lastCol = arr.GetLength(1) - 1, lastRow = arr.GetLength(0) - 1;
while (firstRow != lastRow)
int halfColLength = (lastRow - firstRow) / 2;
int middleRow = halfColLength + firstRow;
if (lastRow < 0 || firstRow >= arr.GetLength(0))
if (arr[middleRow, 0] == num)
if (arr[middleRow,0] > num)
if (arr[middleRow,0] < num && arr[middleRow+1,0] > num)
firstRow = middleRow + 1;
while (firstCol != lastCol)
int halfRowLength = (lastCol - firstCol) / 2;
int middleCol = halfRowLength + firstCol;
if (lastCol < 0 || firstCol >= arr.GetLength(1))
if (arr[firstRow,middleCol] == num)
if (arr[firstRow, middleCol] > num)
firstCol = middleCol + 1;
public static void BubbleSort(int[] arr)
for (int i = 0; i < arr.Length; i++)
for (int j = i + 1; j < arr.Length; j++)
public static void InsertionSort(int[] arr)
for (int i = 1; i < arr.Length; i++)
while (j >= 0 && arr[j] > temp)
public static int Fibonacci (int place)
return Fibonacci(place - 1) + Fibonacci(place - 2);