using System;
|
|
| using System.Collections.Generic;
|
| using System.Linq;
|
| using System.Text;
|
| using System.Threading.Tasks;
|
|
|
| namespace RekursiveSorts
|
| {
|
| public static class ArraySort
|
| {
|
| public static void Show(int[] array)
|
| {
|
| foreach (var el in array)
|
| {
|
| Console.Write(el + " ");
|
| }
|
| Console.WriteLine("\n");
|
| }
|
|
|
|
|
| // 1.BubbleSort
|
|
|
| public static void BubbleSortRecursive(int[] array, int n)
|
| {
|
| if (n == 0)
|
| {
|
| return;
|
| }
|
| for (int j = 0; j < array.Length - 1; j++)
|
| {
|
| if (array[j] > array[j + 1])
|
| {
|
| int temp = array[j + 1];
|
| array[j + 1] = array[j];
|
| array[j] = temp;
|
| }
|
| }
|
| BubbleSortRecursive(array, n - 1);
|
| }
|
|
|
|
|
| // 2.SelectionSort //
|
|
|
| public static void SelectionSortRecursive(int[] array, int n)
|
| {
|
| if (n >= array.Length - 1)
|
| {
|
| return;
|
| }
|
|
|
| int minIndex = n;
|
| for (int i = n + 1; i < array.Length; i++)
|
| {
|
| if (array[i] < array[minIndex])
|
| {
|
| minIndex = i;
|
| }
|
| }
|
| int temp = array[n];
|
| array[n] = array[minIndex];
|
| array[minIndex] = temp;
|
|
|
| SelectionSortRecursive(array, n + 1);
|
| }
|
|
|
| // 3.InsertionSort//
|
|
|
| public static void InsertionSortRecursiveByIndex(int[] a, int i)
|
| {
|
| if (i == a.Length)
|
| {
|
| return;
|
| }
|
| while (i > 0 && a[i] < a[i - 1])
|
| {
|
| int temp = a[i];
|
| a[i] = a[i - 1];
|
| a[i - 1] = temp;
|
| i--;
|
| }
|
|
|
| InsertionSortRecursiveByIndex(a, i + 1);
|
| }
|
|
|
| public static void InsertionSortRecursiveByLength(int[] array, int n)
|
| {
|
| if (n <= 1)
|
| {
|
| return;
|
| }
|
|
|
| InsertionSortRecursiveByLength(array, n - 1);
|
|
|
| int last = array[n - 1];
|
| int j = n - 2;
|
|
|
| while (j >= 0 && array[j] > last)
|
| {
|
| array[j + 1] = array[j];
|
| j--;
|
| }
|
| array[j + 1] = last;
|
| }
|
|
|
|
|
| // 4.MergeSort //
|
|
|
| public static void Mergesort(int[] array, int beg, int end) // imy
|
| {
|
| if (beg == end)
|
| return;
|
|
|
| int mid = (beg + end) / 2;
|
| Mergesort(array, beg, mid);
|
| Mergesort(array, mid + 1, end);
|
|
|
| int i = beg;
|
| int j = mid + 1;
|
| int l = end - beg + 1;
|
| int[] temp = new int[l];
|
| for (int k = 0; k < l; k++)
|
| {
|
| if (j > end || (i <= mid && array[i] < array[j]))
|