Anonim_999

BubbleSorting

Mar 13th, 2021
949
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.91 KB | None | 0 0
  1. using System;
  2. using System.Threading;
  3.  
  4.  
  5. namespace homework
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             while(true)
  12.             {
  13.                 try
  14.                 {
  15.                     WriteColoredText("Enter the size of the items to sort: ");
  16.                     int sizeItems = Convert.ToInt32(Console.ReadLine());
  17.                     WriteColoredText("Enter sorting speed: ");
  18.                     int sortingSpeed = Convert.ToInt32(Console.ReadLine());
  19.                     int[] numbers = new int[sizeItems];
  20.                     numbers = RandomizeArray(numbers);
  21.                     numbers = SortBublle(numbers, sortingSpeed);
  22.                     Console.WriteLine();
  23.  
  24.                     foreach (int item in numbers)
  25.                     {
  26.                         WriteColoredText($"{item} ");
  27.                     }
  28.  
  29.                     WriteColoredText("\n");
  30.                 }
  31.                 catch (System.Exception)
  32.                 {
  33.                     WriteColoredText("Error\n", ConsoleColor.Red);
  34.                 }
  35.             }
  36.            
  37.            
  38.            
  39.         }
  40.  
  41.         private static void WriteColoredText(string text, ConsoleColor color = ConsoleColor.Yellow)
  42.         {
  43.             Console.ForegroundColor = color;
  44.             Console.Write(text);
  45.             Console.ResetColor();
  46.         }
  47.  
  48.         private static void DrawArray(int[] numbers)
  49.         {
  50.             Console.Clear();
  51.             for(int i = 0; i < numbers.Length; i++)
  52.             {
  53.                 for(int j = 0; j < numbers[i]; j++)
  54.                 {
  55.                     WriteColoredText("#");
  56.                 }
  57.                 Console.WriteLine();
  58.             }
  59.         }
  60.  
  61.         private static int[] RandomizeArray(int[] array)
  62.         {
  63.             Random rand = new Random();
  64.             int[] randomizeArray = new int[array.Length];
  65.            
  66.             for(int i = 0; i < array.Length; i++)
  67.             {
  68.                 randomizeArray[i] = rand.Next(1,11);
  69.             }
  70.             return randomizeArray;
  71.         }
  72.  
  73.         private static int[] SortBublle(int[] array, int sortingSpeed)
  74.         {
  75.             int[] sortArray = array;
  76.             int timeSleep = sortingSpeed;
  77.             for (int i = 0; i < sortArray.Length; i++)
  78.             {
  79.                 for (int sort = 0; sort < sortArray.Length - 1; sort++)
  80.                 {
  81.                     if (sortArray[sort] > sortArray[sort + 1])
  82.                     {
  83.                         Thread.Sleep(timeSleep);
  84.                         int temp = sortArray[sort + 1];
  85.                         sortArray[sort + 1] = sortArray[sort];
  86.                         sortArray[sort] = temp;
  87.                         DrawArray(sortArray);
  88.                        
  89.                     }      
  90.                 }
  91.             }  
  92.             return sortArray;
  93.         }
  94.     }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment