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 Сортировка_в_одномерном_массиве
- {
- public struct StrArray
- {
- private int intSize;
- private short[] arrItems;
- private int intCurIndex;
- public StrArray(int NewSize) //Конструктор структуры StrArray
- {
- intSize = NewSize;
- arrItems = new short[intSize];
- intCurIndex = 0;
- }
- public int GetFreeSpace()
- {
- return intSize - intCurIndex;
- }
- public void PrintArray() //Вывести массив на экран
- {
- for (int intC = 0; intC < intCurIndex; intC++)
- Console.Write(arrItems[intC] + " ");
- Console.WriteLine();
- }
- public bool AddNewElement(short newItem) //Добавление нового элемента
- {
- if (intCurIndex != intSize)
- {
- arrItems[intCurIndex] = newItem;
- intCurIndex++;
- return true;
- }
- return false;
- }
- public bool DeleteWithoutNewArray(int delIndex) //Удаление элемента по индексу
- {
- if ((delIndex > intCurIndex)||(delIndex<0))
- return false;
- short tmp;
- for (int intC = delIndex; intC < intSize-1; intC++)
- {
- tmp = arrItems[intC];
- arrItems[intC] = arrItems[intC + 1];
- arrItems[intC + 1] = tmp;
- }
- intCurIndex--;
- return true;
- }
- public void SelectionSort() //Сортировка выбором
- {
- int intMinIndex;
- short tmp;
- for(int intC = 0; intC < intCurIndex; intC++)
- {
- intMinIndex = intC;
- for (int intD = intC + 1; intD <intCurIndex; intD++)
- {
- if (arrItems[intD] < arrItems[intMinIndex])
- intMinIndex = intD;
- }
- tmp = arrItems[intC];
- arrItems[intC] = arrItems[intMinIndex];
- arrItems[intMinIndex] = tmp;
- }
- }
- public bool isSorted() //Проверка массива на отсортированность
- {
- bool SortCheck = true;
- for (int intC = 0; intC < intCurIndex-1; intC++)
- if (arrItems[intC] > arrItems[intC + 1])
- SortCheck = false;
- return SortCheck;
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- short shInput;
- int intInput;
- StrArray Arr1 = new StrArray(5);
- System.ConsoleKey Selection;
- do
- {
- Console.Clear();
- Console.WriteLine("~~~ Одномерный массив ~~~");
- Console.WriteLine("Нажмите соответствующую кнопку для вызова той или иной функции:");
- Console.WriteLine("[1] - Добавить новый элемент в массив");
- Console.WriteLine("[2] - Удалить элемент по индексу");
- Console.WriteLine("[3] - Вывести весь массив");
- Console.WriteLine("[4] - Отстортировать");
- Console.WriteLine("[5] - Выход");
- Selection = Console.ReadKey(true).Key;
- if (Selection == ConsoleKey.D1)
- {
- Console.Write("Введите значение, которое необходимо добавить в массив:");
- shInput = short.Parse(Console.ReadLine());
- if (Arr1.AddNewElement(shInput))
- Console.WriteLine("Элемент был успешно добавлен, количество свободных ячеек: {0}",Arr1.GetFreeSpace());
- else
- Console.WriteLine("Первышено число элементов массива. Новы элемент не был добавлен");
- Console.WriteLine("Нажмите любую клавишу для продолжения...");
- Console.ReadKey();
- }
- if (Selection == ConsoleKey.D2)
- {
- Console.Write("Введите индекс, значение которого необходимо удалить:");
- intInput=int.Parse(Console.ReadLine());
- if (Arr1.DeleteWithoutNewArray(intInput))
- Console.WriteLine("Элемент с индексом {0} был успешно удален", intInput);
- else
- Console.WriteLine("Неверно указан индекс элемента");
- Console.WriteLine("Нажмите любую клавишу для продолжения...");
- Console.ReadKey();
- }
- if (Selection == ConsoleKey.D3)
- {
- Arr1.PrintArray();
- Console.WriteLine("Нажмите любую клавишу для продолжения...");
- Console.ReadKey();
- }
- if (Selection == ConsoleKey.D4)
- {
- if (Arr1.isSorted())
- Console.WriteLine("Массив не нуждается в сортировке");
- else
- {
- Arr1.SelectionSort();
- if (Arr1.isSorted())
- Console.WriteLine("Массив был успешно отсортирован");
- }
- Console.WriteLine("Нажмите любую клавишу для продолжения...");
- Console.ReadKey();
- }
- } while (Selection != ConsoleKey.D5);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement