Advertisement
0xPaulius

Sorts

Dec 7th, 2022
681
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.42 KB | None | 0 0
  1. using System.Globalization;
  2.  
  3. string ans = "";
  4. int num = 0;
  5. int[] numbers;
  6. int[] selection;
  7. int[] insertion;
  8. int[] bubble;
  9. while (true)
  10. {
  11.     Console.Write("Enter number of elements: ");
  12.     ans = Console.ReadLine();
  13.     if (int.TryParse(ans, out num))
  14.     {
  15.         if (num > 1)
  16.         {
  17.             numbers = MakeArray(num);
  18.             PrintNumbers(numbers, "Numbers:");
  19.             numbers = ShuffleArray(numbers);
  20.             PrintNumbers(numbers, "Shuffled numbers:");
  21.             selection = numbers.ToArray();
  22.             insertion = numbers.ToArray();
  23.             bubble = numbers.ToArray();
  24.             SelectionSort(selection);
  25.             VisualInsertionSort(insertion);
  26.             VisualBubbleSort(bubble);
  27.             BogoSort(numbers);
  28.             break;
  29.         }
  30.         else
  31.         {
  32.             Console.WriteLine("Number must be bigger than 1");
  33.         }
  34.     }
  35.     else
  36.     {
  37.         Console.WriteLine("You must write a full number");
  38.     }
  39. }
  40.  
  41. int[] MakeArray(int length)
  42. {
  43.     Random rnd = new Random();
  44.     int[] array = new int[length];
  45.     for (int i = 0; i < length; i++)
  46.     {
  47.         array[i] = i + 1;
  48.     }
  49.     return array;
  50. }
  51.  
  52. void PrintNumbers(int[] array, string title)
  53. {
  54.     Console.WriteLine("\n" + title);
  55.     foreach (int num in array)
  56.     {
  57.         Console.Write(num + " ");
  58.     }
  59.     Console.WriteLine("");
  60. }
  61.  
  62. int[] ShuffleArray(int[] array)
  63. {
  64.     Random rand = new Random();
  65.     int temp;
  66.     int rnd;
  67.     if (array.Length == 2)
  68.     {
  69.         temp = array[0];
  70.         array[0] = array[1];
  71.         array[1] = temp;
  72.     }
  73.     else
  74.     {
  75.         for (int i = 0; i < array.Length; i++)
  76.         {
  77.             while (true)
  78.             {
  79.                 rnd = rand.Next(0, array.Length);
  80.                 if (rnd != i)
  81.                 {
  82.                     break;
  83.                 }
  84.             }
  85.             temp = array[i];
  86.             array[i] = array[rnd];
  87.             array[rnd] = temp;
  88.         }
  89.     }
  90.     return array;
  91. }
  92.  
  93. void SelectionSort(int[] array)
  94. {
  95.     Random rand = new Random();
  96.     int temp, smallest;
  97.     int n = array.Length;
  98.     int index = 0;
  99.     Console.WriteLine("\nSelection sort visualization");
  100.     PrintLines(array);
  101.     while (!IsSorted(array))
  102.     {
  103.         for (int i = 0; i < n; i++)
  104.         {
  105.             smallest = i;
  106.             for (int j = i + 1; j < n; j++)
  107.             {
  108.                 if (array[j] < array[smallest])
  109.                 {
  110.                     smallest = j;
  111.                     index++;
  112.                 }
  113.  
  114.             }
  115.             RemoveLines(array.Length, Console.CursorTop);
  116.             PrintLines(array);
  117.             temp = array[smallest];
  118.             array[smallest] = array[i];
  119.             array[i] = temp;
  120.         }
  121.     }
  122.     Console.WriteLine("\nAfter {0} switches, lines are sorted", index);
  123. }
  124.  
  125. void PrintLines(int[] lengths)
  126. {
  127.     for (int i = 0; i < lengths.Length; i++)
  128.     {
  129.         Console.WriteLine();
  130.         Console.WriteLine("  " + lengths[i] + "  " + new String('█', lengths[i] * 6));
  131.     }
  132. }
  133.  
  134. void RemoveLines(int num, int endPos)
  135. {
  136.     Thread.Sleep(200);
  137.     for (int i = 0; i < num; i++)
  138.     {
  139.         Console.SetCursorPosition(0, endPos - (i * 2) - 1);
  140.         Console.WriteLine(new String(' ', 100));
  141.     }
  142.     Console.SetCursorPosition(0, endPos - (num * 2));
  143. }
  144.  
  145. bool IsSorted(int[] lengths)
  146. {
  147.     for (int i = 0; i < lengths.Length - 1; i++)
  148.     {
  149.         if (lengths[i] > lengths[i + 1])
  150.         {
  151.             return false;
  152.         }
  153.     }
  154.     return true;
  155. }
  156.  
  157. void VisualBubbleSort(int[] array)
  158. {
  159.     Random rand = new Random();
  160.     int temp;
  161.     int index = 0;
  162.     Console.WriteLine("\nBubble sort visualization");
  163.     PrintLines(array);
  164.     while (!IsSorted(array))
  165.     {
  166.         for (int i = 0; i < array.Length - 1; i++)
  167.         {
  168.             if (array[i] > array[i + 1])
  169.             {
  170.                 temp = array[i];
  171.                 array[i] = array[i + 1];
  172.                 array[i + 1] = temp;
  173.                 index++;
  174.                 RemoveLines(array.Length, Console.CursorTop);
  175.                 PrintLines(array);
  176.             }
  177.         }
  178.     }
  179.     Console.WriteLine("\nAfter {0} switches, lines are sorted", index);
  180. }
  181.  
  182. void VisualInsertionSort(int[] array)
  183. {
  184.     Random rand = new Random();
  185.     int n = array.Length, i, j, val, flag;
  186.     int index = 0;
  187.     Console.WriteLine("\nInsertion sort visualization");
  188.     PrintLines(array);
  189.     for (i = 1; i < n; i++)
  190.     {
  191.         val = array[i];
  192.         flag = 0;
  193.         for (j = i - 1; j >= 0 && flag != 1;)
  194.         {
  195.             if (val < array[j])
  196.             {
  197.                 array[j + 1] = array[j];
  198.                 j--;
  199.                 array[j + 1] = val;
  200.                 index++;
  201.                 RemoveLines(array.Length, Console.CursorTop);
  202.                 PrintLines(array);
  203.             }
  204.             else flag = 1;
  205.         }
  206.     }
  207.     Console.WriteLine("\nAfter {0} switches, lines are sorted", index);
  208.  
  209. }
  210.  
  211. void BogoSort(int[] array)
  212. {
  213.     int index = 0;
  214.     Console.WriteLine("\nBogo sort visualization");
  215.     PrintLines(array);
  216.     while (!IsSorted(array))
  217.     {
  218.         RemoveLines(array.Length, Console.CursorTop);
  219.         PrintLines(array);
  220.         index++;
  221.         array = ShuffleArray(array);
  222.     }
  223.     RemoveLines(array.Length, Console.CursorTop);
  224.     PrintLines(array);
  225.     Console.WriteLine("\nAfter {0} switches, lines are sorted", index);
  226. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement