Advertisement
Danvil

Сортировка в одномерном массиве

Mar 13th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.36 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 Сортировка_в_одномерном_массиве
  8. {
  9.     public struct StrArray
  10.     {
  11.         private int intSize;
  12.         private short[] arrItems;
  13.         private int intCurIndex;
  14.  
  15.        
  16.         public StrArray(int NewSize) //Конструктор структуры StrArray
  17.         {
  18.             intSize = NewSize;
  19.             arrItems = new short[intSize];
  20.             intCurIndex = 0;
  21.         }
  22.         public int GetFreeSpace()
  23.         {
  24.             return intSize - intCurIndex;
  25.         }
  26.         public void PrintArray() //Вывести массив на экран
  27.         {
  28.             for (int intC = 0; intC < intCurIndex; intC++)
  29.                 Console.Write(arrItems[intC] + " ");
  30.             Console.WriteLine();
  31.         }
  32.         public bool AddNewElement(short newItem) //Добавление нового элемента
  33.         {
  34.             if (intCurIndex != intSize)
  35.             {
  36.                 arrItems[intCurIndex] = newItem;
  37.                 intCurIndex++;
  38.                 return true;
  39.             }
  40.             return false;
  41.         }
  42.         public bool DeleteWithoutNewArray(int delIndex)  //Удаление элемента по индексу
  43.         {
  44.             if ((delIndex > intCurIndex)||(delIndex<0))
  45.                 return false;
  46.  
  47.             short tmp;
  48.             for (int intC = delIndex; intC < intSize-1; intC++)
  49.             {
  50.                 tmp = arrItems[intC];
  51.                 arrItems[intC] = arrItems[intC + 1];
  52.                 arrItems[intC + 1] = tmp;
  53.             }
  54.             intCurIndex--;
  55.             return true;
  56.         }
  57.         public void SelectionSort() //Сортировка выбором
  58.         {
  59.             int intMinIndex;
  60.             short tmp;
  61.             for(int intC = 0; intC < intCurIndex; intC++)
  62.             {
  63.                 intMinIndex = intC;
  64.                 for (int intD = intC + 1; intD <intCurIndex; intD++)
  65.                 {
  66.                     if (arrItems[intD] < arrItems[intMinIndex])
  67.                         intMinIndex = intD;
  68.                 }
  69.                 tmp = arrItems[intC];
  70.                 arrItems[intC] = arrItems[intMinIndex];
  71.                 arrItems[intMinIndex] = tmp;
  72.             }
  73.         }
  74.         public bool isSorted() //Проверка массива на отсортированность
  75.         {
  76.             bool SortCheck = true;
  77.             for (int intC = 0; intC < intCurIndex-1; intC++)
  78.                 if (arrItems[intC] > arrItems[intC + 1])
  79.                     SortCheck = false;            
  80.             return SortCheck;
  81.         }
  82.  
  83.     }
  84.     class Program
  85.     {
  86.        
  87.         static void Main(string[] args)
  88.         {
  89.             short shInput;
  90.             int intInput;
  91.             StrArray Arr1 = new StrArray(5);
  92.             System.ConsoleKey Selection;
  93.             do
  94.             {
  95.                 Console.Clear();
  96.                 Console.WriteLine("~~~ Одномерный массив ~~~");
  97.                 Console.WriteLine("Нажмите соответствующую кнопку для вызова той или иной функции:");
  98.                 Console.WriteLine("[1] - Добавить новый элемент в массив");
  99.                 Console.WriteLine("[2] - Удалить элемент по индексу");
  100.                 Console.WriteLine("[3] - Вывести весь массив");
  101.                 Console.WriteLine("[4] - Отстортировать");
  102.                 Console.WriteLine("[5] - Выход");
  103.                 Selection = Console.ReadKey(true).Key;
  104.                 if (Selection == ConsoleKey.D1)
  105.                 {
  106.                     Console.Write("Введите значение, которое необходимо добавить в массив:");
  107.                     shInput = short.Parse(Console.ReadLine());
  108.                     if (Arr1.AddNewElement(shInput))
  109.                         Console.WriteLine("Элемент был успешно добавлен, количество свободных ячеек: {0}",Arr1.GetFreeSpace());
  110.                     else
  111.                         Console.WriteLine("Первышено число элементов массива. Новы элемент не был добавлен");
  112.                     Console.WriteLine("Нажмите любую клавишу для продолжения...");
  113.                     Console.ReadKey();
  114.                 }
  115.                    
  116.                 if (Selection == ConsoleKey.D2)
  117.                 {
  118.                     Console.Write("Введите индекс, значение которого необходимо удалить:");
  119.                     intInput=int.Parse(Console.ReadLine());
  120.                     if (Arr1.DeleteWithoutNewArray(intInput))
  121.                         Console.WriteLine("Элемент с индексом {0} был успешно удален", intInput);
  122.                     else
  123.                         Console.WriteLine("Неверно указан индекс элемента");
  124.                     Console.WriteLine("Нажмите любую клавишу для продолжения...");
  125.                     Console.ReadKey();
  126.                 }
  127.  
  128.                 if (Selection == ConsoleKey.D3)
  129.                 {
  130.                     Arr1.PrintArray();
  131.                     Console.WriteLine("Нажмите любую клавишу для продолжения...");
  132.                     Console.ReadKey();
  133.                 }
  134.                 if (Selection == ConsoleKey.D4)
  135.                 {
  136.                     if (Arr1.isSorted())
  137.                         Console.WriteLine("Массив не нуждается в сортировке");
  138.                     else
  139.                     {
  140.                         Arr1.SelectionSort();
  141.                         if (Arr1.isSorted())
  142.                             Console.WriteLine("Массив был успешно отсортирован");
  143.                     }
  144.                     Console.WriteLine("Нажмите любую клавишу для продолжения...");
  145.                     Console.ReadKey();
  146.  
  147.  
  148.                 }
  149.              
  150.             } while (Selection != ConsoleKey.D5);
  151.  
  152.  
  153.         }
  154.     }
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement