Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ShakeSort_CSharp
- {
- class Program
- {
- static void Main()
- {
- Sort();
- }
- static void Sort()
- {
- int[] array = { 99, 88, 77, 66, 55, 44, 33, 22, 11, 8, 5, 3, 1 }; // Реализовать ввод в ручную???
- WriteArray(array);
- ShakerSort(array);
- WriteArray(array);
- Console.ReadLine();
- }
- /* Шейкер-сортировка */
- static void ShakerSort(int[] array)
- {
- int beginOf, endOf;
- int count = 0;
- for (int i = 0; i < array.Length / 2; i++)
- {
- beginOf = 0;
- endOf = array.Length - 1;
- do
- {
- count += 2;
- if (array[beginOf] > array[beginOf + 1])
- Swap(array, beginOf, beginOf + 1);
- beginOf++;
- if (array[endOf - 1] > array[endOf])
- Swap(array, endOf - 1, endOf);
- endOf--;
- }
- while (beginOf <= endOf);
- }
- Console.Write("\nКоличество сравнений = {0}\n\n\n", count);
- }
- static void Swap(int[] array, int i, int j)
- {
- int glass;
- glass = array[i];
- array[i] = array[j];
- array[j] = glass;
- }
- static void WriteArray(int[] a)
- {
- foreach (int i in a)
- Console.Write("{0}\t", i);
- Console.Write("\n\n");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement