Advertisement
Guest User

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

a guest
Sep 16th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.85 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.     private static void BubbleSort(int[] array)
  9.     {
  10.         for (int i = 0; i < array.Length; i++)
  11.         for (int j = 0; j < array.Length - 1; j++)
  12.             if (array[j] > array[j + 1])
  13.             {
  14.                 int t = array[j + 1];
  15.                 array[j + 1] = array[j];
  16.                 array[j] = t;
  17.             }
  18.     }
  19.  
  20.     public static void Main()
  21.     {
  22.         Stopwatch sWatch = new Stopwatch();
  23.         sWatch.Start();
  24.         int[] array = GenerateArray(20);
  25.         BubbleSort(array);
  26.         foreach (int e in array)
  27.             Console.WriteLine(e);
  28.         UnElementsInMassive(array);
  29.         Console.WriteLine();
  30.         SearchInMassive(array);
  31.         sWatch.Stop();
  32.         Console.WriteLine("Время в миллисекундах " + sWatch.ElapsedMilliseconds.ToString());
  33.         Console.ReadKey();
  34.     }
  35.  
  36.     private static int[] GenerateArray(int length)
  37.     {
  38.         var array = new int[length];
  39.         for (int i = 0; i < array.Length; i++)
  40.             array[i] = Random.Next();
  41.         return array;
  42.     }
  43.  
  44.     private static void UnElementsInMassive(int[] array)
  45.     {
  46.         int UnElements = 0;
  47.         foreach (int e in array)
  48.         {
  49.             if (e % 2 == 0)
  50.             {
  51.                 UnElements++;
  52.             }
  53.         }
  54.  
  55.         Console.WriteLine();
  56.         Console.WriteLine(UnElements);
  57.     }
  58.  
  59.     private static void SearchInMassive(int[] array)
  60.     {
  61.         int indexOfMassive = int.Parse(Console.ReadLine());
  62.         if (indexOfMassive > array.Length)
  63.         {
  64.             Console.WriteLine("Ты шо ебобо? Давай поменьше");
  65.         }
  66.  
  67.         Console.WriteLine();
  68.         Console.WriteLine(array[indexOfMassive]);
  69.     }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement