Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Collections;
- namespace sortCoctail
- {
- internal class Program
- {
- static void Main(string[] args)
- {
- Console.WriteLine("Введите неотсортированный массив");
- int[] arr = new int[10];
- for (int i = 0; i < arr.Length; i++)
- {
- arr[i] = Convert.ToInt32(Console.ReadLine());
- }
- Console.WriteLine("\n\n");
- int[] result = new int[arr.Length];
- result = ShakerSort(arr);
- Console.WriteLine("\n\nОтсортированный массив\n");
- foreach (int i in result)
- {
- Console.WriteLine(i);
- }
- }
- static int[] ShakerSort(int[] arr)
- {
- int left = 0;
- int right = arr.Length - 1;
- int count = 0;
- while (left < right || count == 0)
- {
- for(int i = left; i < right; i++)
- {
- count++;
- if(arr[i] > arr[i + 1])
- {
- Swap(arr, i, i + 1);
- }
- }
- right--;
- for(int i = right; i > left; i--)
- {
- count++;
- if(arr[i - 1] > arr[i])
- {
- Swap(arr, i - 1, i);
- }
- }
- left++;
- }
- Console.WriteLine("\nКоличество сравнений = {0}", count.ToString());
- return arr;
- }
- static void Swap(int[] arr, int i, int j)
- {
- int temp = arr[i];
- arr[i] = arr[j];
- arr[j] = temp;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement