Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- using System.Collections;
- using System.Collections.Generic;
- namespace Lab_16
- {
- class MainClass
- {
- // Объявление переменных
- static int[] numbers;
- static int[] qSortNumbers;
- static int[] bubbleSort;
- static int size;
- // Сортировка пузырьком
- public static int[] BubbleSort(int[] arr)
- {
- int[] res = arr;
- int temp;
- for (int i = 0; i < arr.Length; i++)
- {
- for (int j = i + 1; j < arr.Length; j++)
- {
- if (res[i] > res[j])
- {
- temp = res[i];
- res[i] = res[j];
- res[j] = temp;
- }
- }
- }
- return res;
- }
- // Сортировка QSort
- static void QuickSort(int[] arr, int l, int r)
- {
- if (l >= r)
- {
- return;
- }
- int temp;
- int m = l;
- for (int i = m; i < r; i++)
- {
- if (arr[i] <= arr[r])
- {
- temp = arr[m];
- arr[m] = arr[i];
- arr[i] = temp;
- m++;
- }
- }
- temp = arr[m];
- arr[m] = arr[r];
- arr[r] = temp;
- QuickSort(arr, l, m - 1);
- QuickSort(arr, m + 1, r);
- }
- // Удаление повторяющихся элементов из массива
- public static int[] deleteRepetititve(int[] arr)
- {
- ArrayList list = new ArrayList();
- for (int i = 0; i < arr.Length; i++)
- {
- if (!list.Contains(arr[i]))
- {
- list.Add(arr[i]);
- }
- }
- int[] res = (Int32[])list.ToArray(typeof(int));
- return res;
- }
- // Отображение массива и его описания
- public static void DisplayArray(int[] arr, String arrOperation)
- {
- Console.WriteLine(arrOperation);
- for (int i = 0; i < arr.Length; i++)
- {
- Console.Write(arr[i] + " ");
- }
- Console.WriteLine();
- }
- // Отображение указанных пользователем минимума и максимума
- public static void DisplayMinAndMax(int[] arr)
- {
- int m, n;
- int max = Int32.MinValue;
- int maxCounter = 0;
- int min = Int32.MaxValue;
- int minCounter = 0;
- try
- {
- List<int> listmax = new List<int>();
- List<int> listmin = new List<int>();
- Console.WriteLine("Enter necessary min");
- m = EnterInt(3);
- Console.WriteLine("Enter necessary high");
- n = EnterInt(3);
- listmax = arr.ToList();
- for (int i = 0; i < n; i++)
- {
- max = Int32.MinValue;
- for (int j = 0; j < listmax.Count(); j++)
- {
- if (listmax[j] > max)
- {
- max = listmax[j];
- maxCounter = j;
- }
- }
- if (i != n - 1)
- {
- listmax.Remove(max);
- }
- }
- Console.WriteLine(n + "-й максимум " + listmax[maxCounter]);
- listmin = arr.ToList();
- for (int i = 0; i < m; i++)
- {
- min = Int32.MaxValue;
- for (int j = 0; j < listmin.Count(); j++)
- {
- if (listmin[j] < min)
- {
- min = listmin[j];
- minCounter = j;
- }
- }
- if (i != m - 1)
- {
- listmin.Remove(min);
- }
- }
- Console.WriteLine(m + "-й минимум " + listmin[minCounter]);
- }
- catch (Exception e)
- {
- Console.WriteLine("Sorry, an error was occured: " + e);
- return;
- }
- }
- // Основная часть программы и ее дополнительные компоненты
- public static void ProcessingArray()
- {
- DisplayArray(BubbleSort(bubbleSort), "Sorted array (Bubble sort)");
- QuickSort(qSortNumbers, 0, qSortNumbers.Length - 1);
- DisplayArray(qSortNumbers, "Sorted array (QSort)");
- DisplayArray(deleteRepetititve(numbers), "Deleted repetitives");
- DisplayMinAndMax(deleteRepetititve(numbers));
- Console.WriteLine("Press any key to continue...");
- }
- public static int EnterInt(int type)
- {
- int a;
- switch (type)
- {
- case 1:
- {
- try
- {
- a = Int32.Parse(Console.ReadLine());
- return a;
- }
- catch (Exception)
- {
- Console.WriteLine("Sorry, an error was occured, please try again: ");
- a = EnterInt(1);
- return a;
- }
- }
- break;
- case 2:
- {
- try
- {
- a = Int32.Parse(Console.ReadLine());
- if (a > 0 & a <= 100)
- {
- return a;
- } else
- {
- Console.WriteLine("Sorry, an error was occured, please try again: ");
- a = EnterInt(2);
- return a;
- }
- }
- catch (Exception)
- {
- Console.WriteLine("Sorry, an error was occured, please try again: ");
- a = EnterInt(2);
- return a;
- }
- }
- break;
- case 3:
- {
- try
- {
- a = Int32.Parse(Console.ReadLine());
- if (a > 0 & a < size)
- {
- return a;
- }
- else
- {
- Console.WriteLine("Sorry, an error was occured, please try again: ");
- a = EnterInt(3);
- return a;
- }
- }
- catch (Exception)
- {
- Console.WriteLine("Sorry, an error was occured, please try again: ");
- a = EnterInt(3);
- return a;
- }
- }
- break;
- case 4:
- try
- {
- a = Int32.Parse(Console.ReadLine());
- if (a == 1 | a == 2)
- {
- return a;
- }
- else
- {
- Console.WriteLine("Sorry, an error was occured, please try again: ");
- a = EnterInt(4);
- return a;
- }
- }
- catch (Exception)
- {
- Console.WriteLine("Sorry, an error was occured, please try again: ");
- a = EnterInt(4);
- return a;
- }
- break;
- default:
- {
- return 0;
- }
- break;
- }
- }
- public static void Main(string[] args)
- {
- Console.WriteLine("Enter array size (max is 100)");
- size = EnterInt(2);
- numbers = new int[size];
- qSortNumbers = new int[size];
- bubbleSort = new int[size];
- Random random = new Random();
- Console.WriteLine("Choose input type: 1 for auto, 2 for manual");
- switch (EnterInt(4))
- {
- case 1:
- Console.WriteLine("Your array:");
- for (int i = 0; i < numbers.Length; i++)
- {
- numbers[i] = random.Next(-100, 101);
- qSortNumbers[i] = numbers[i];
- bubbleSort[i] = numbers[i];
- Console.Write(numbers[i] + " ");
- }
- Console.WriteLine();
- ProcessingArray();
- break;
- case 2:
- for (int i = 0; i < numbers.Length; i++)
- {
- numbers[i] = EnterInt(1);
- qSortNumbers[i] = numbers[i];
- bubbleSort[i] = numbers[i];
- }
- ProcessingArray();
- break;
- default:
- Console.WriteLine("Sorry, an unknown error was occured");
- break;
- }
- Console.ReadLine();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement