Advertisement
Guest User

Три алгоритма (время выполнения UPD)

a guest
Sep 20th, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.67 KB | None | 0 0
  1. using System;
  2. using System.Diagnostics;
  3.  
  4. class Program
  5. {
  6.     private static readonly Random Random = new Random();
  7.    
  8.     public static void Main()
  9.     {
  10.         int[] array = GenerateArray(100);
  11.         long timeStart = DateTime.Now.Ticks / 10000;
  12.         BubbleSort(array);
  13.         Console.WriteLine("Время в миллисекундах после сортировки массива " + (DateTime.Now.Ticks / 10000 - timeStart));
  14.         timeStart = DateTime.Now.Ticks / 10000;
  15.         UnElementsInArray(array);
  16.         Console.WriteLine("Время в миллисекундах после поиска количества чётных чисел в массиве " +
  17.                           (DateTime.Now.Ticks / 10000 - timeStart));
  18.         Console.WriteLine();
  19.         int findElem = GetElemReadLine(array.Length);
  20.         timeStart = DateTime.Now.Ticks / 10000;
  21.         SearchInMassive(array, findElem);
  22.         Console.WriteLine("Время в миллисекундах работы алгоритма " + (DateTime.Now.Ticks / 10000 - timeStart));
  23.     }
  24.     private static void BubbleSort(int[] array)
  25.     {
  26.         for (int i = 0; i < array.Length; i++)
  27.         for (int j = 0; j < array.Length - 1; j++)
  28.             if (array[j] > array[j + 1])
  29.             {
  30.                 int t = array[j + 1];
  31.                 array[j + 1] = array[j];
  32.                 array[j] = t;
  33.             }
  34.     }
  35.    
  36.     private static int GetElemReadLine(int arrayLength)
  37.     {
  38.         Console.WriteLine("Вводи индекс для поиска");
  39.         int resElem;
  40.         bool readedLine = false;
  41.         do
  42.         {
  43.             if(readedLine)
  44.                 Console.WriteLine("ъуъ, давай поадекватнее индекс");
  45.             readedLine = true;
  46.             resElem = int.Parse(Console.ReadLine());
  47.         } while (resElem < 0 || resElem >= arrayLength);
  48.         return resElem;
  49.     }
  50.  
  51.     private static int[] GenerateArray(int length)
  52.     {
  53.         var array = new int[length];
  54.         for (int i = 0; i < array.Length; i++)
  55.             array[i] = Random.Next();
  56.         return array;
  57.     }
  58.  
  59.     private static void UnElementsInArray(int[] array)
  60.     {
  61.         int unElements = 0;
  62.         foreach (int e in array)
  63.         {
  64.             if (e % 2 == 0)
  65.             {
  66.                 unElements++;
  67.             }
  68.         }
  69.         Console.WriteLine($"Количество чётных чисел {unElements}");
  70.     }
  71.  
  72.     private static void SearchInMassive(int[] array, int findElem)
  73.     {
  74.         int indexOfMassive = findElem;
  75.         Console.WriteLine();
  76.         Console.WriteLine(array[indexOfMassive]);
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement