Advertisement
Guest User

Untitled

a guest
Feb 18th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.74 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ShakeSort_CSharp
  8. {
  9.     class Program
  10.     {
  11.  
  12.         static void Main()
  13.         {
  14.             Sort();
  15.         }
  16.  
  17.         static void Sort()
  18.         {
  19.  
  20.             int[] array = { 99, 88, 77, 66, 55, 44, 33, 22, 11, 8, 5, 3, 1 }; // Реализовать ввод в ручную???
  21.  
  22.             WriteArray(array);
  23.             ShakerSort(array);
  24.             WriteArray(array);
  25.  
  26.             Console.ReadLine();
  27.  
  28.         }
  29.  
  30.         /* Шейкер-сортировка */
  31.         static void ShakerSort(int[] array)
  32.         {
  33.             int beginOf, endOf;
  34.             int count = 0;
  35.  
  36.             for (int i = 0; i < array.Length / 2; i++)
  37.             {
  38.                 beginOf = 0;
  39.                 endOf = array.Length - 1;
  40.  
  41.                 do
  42.                 {
  43.                     count += 2;
  44.  
  45.                     if (array[beginOf] > array[beginOf + 1])
  46.                         Swap(array, beginOf, beginOf + 1);
  47.                     beginOf++;
  48.  
  49.  
  50.                    
  51.                     if (array[endOf - 1] > array[endOf])
  52.                         Swap(array, endOf - 1, endOf);
  53.                     endOf--;
  54.  
  55.                 }
  56.                 while (beginOf <= endOf);
  57.             }
  58.             Console.Write("\nКоличество сравнений = {0}\n\n\n", count);
  59.         }
  60.  
  61.  
  62.  
  63.         static void Swap(int[] array, int i, int j)
  64.         {
  65.             int glass;
  66.             glass = array[i];
  67.             array[i] = array[j];
  68.             array[j] = glass;
  69.         }
  70.  
  71.         static void WriteArray(int[] a)
  72.         {
  73.             foreach (int i in a)
  74.                 Console.Write("{0}\t", i);
  75.             Console.Write("\n\n");
  76.         }
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement