Advertisement
m1okgoodyes

sortCocktail

Apr 8th, 2022
953
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.87 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections;
  4.  
  5.  
  6. namespace sortCoctail
  7. {
  8.     internal class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             Console.WriteLine("Введите неотсортированный массив");
  13.             int[] arr = new int[10];
  14.             for (int i = 0; i < arr.Length; i++)
  15.             {
  16.                 arr[i] = Convert.ToInt32(Console.ReadLine());
  17.             }
  18.  
  19.             Console.WriteLine("\n\n");
  20.             int[] result = new int[arr.Length];
  21.             result = ShakerSort(arr);
  22.             Console.WriteLine("\n\nОтсортированный массив\n");
  23.             foreach (int i in result)
  24.             {
  25.                 Console.WriteLine(i);
  26.             }
  27.         }
  28.         static int[] ShakerSort(int[] arr)
  29.         {
  30.             int left = 0;
  31.             int right = arr.Length - 1;
  32.             int count = 0;
  33.             while (left < right || count == 0)
  34.             {
  35.                 for(int i = left; i < right; i++)
  36.                 {
  37.                     count++;
  38.                     if(arr[i] > arr[i + 1])
  39.                     {
  40.                         Swap(arr, i, i + 1);
  41.                     }
  42.                 }
  43.                 right--;
  44.  
  45.                 for(int i = right; i > left; i--)
  46.                 {
  47.                     count++;
  48.                     if(arr[i - 1] > arr[i])
  49.                     {
  50.                         Swap(arr, i - 1, i);
  51.                     }
  52.                 }
  53.                 left++;
  54.             }
  55.             Console.WriteLine("\nКоличество сравнений = {0}", count.ToString());
  56.             return arr;
  57.         }
  58.  
  59.         static void Swap(int[] arr, int i, int j)
  60.         {
  61.             int temp = arr[i];
  62.             arr[i] = arr[j];
  63.             arr[j] = temp;
  64.         }
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement