Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Globalization;
- string ans = "";
- int num = 0;
- int[] numbers;
- int[] selection;
- int[] insertion;
- int[] bubble;
- while (true)
- {
- Console.Write("Enter number of elements: ");
- ans = Console.ReadLine();
- if (int.TryParse(ans, out num))
- {
- if (num > 1)
- {
- numbers = MakeArray(num);
- PrintNumbers(numbers, "Numbers:");
- numbers = ShuffleArray(numbers);
- PrintNumbers(numbers, "Shuffled numbers:");
- selection = numbers.ToArray();
- insertion = numbers.ToArray();
- bubble = numbers.ToArray();
- SelectionSort(selection);
- VisualInsertionSort(insertion);
- VisualBubbleSort(bubble);
- BogoSort(numbers);
- break;
- }
- else
- {
- Console.WriteLine("Number must be bigger than 1");
- }
- }
- else
- {
- Console.WriteLine("You must write a full number");
- }
- }
- int[] MakeArray(int length)
- {
- Random rnd = new Random();
- int[] array = new int[length];
- for (int i = 0; i < length; i++)
- {
- array[i] = i + 1;
- }
- return array;
- }
- void PrintNumbers(int[] array, string title)
- {
- Console.WriteLine("\n" + title);
- foreach (int num in array)
- {
- Console.Write(num + " ");
- }
- Console.WriteLine("");
- }
- int[] ShuffleArray(int[] array)
- {
- Random rand = new Random();
- int temp;
- int rnd;
- if (array.Length == 2)
- {
- temp = array[0];
- array[0] = array[1];
- array[1] = temp;
- }
- else
- {
- for (int i = 0; i < array.Length; i++)
- {
- while (true)
- {
- rnd = rand.Next(0, array.Length);
- if (rnd != i)
- {
- break;
- }
- }
- temp = array[i];
- array[i] = array[rnd];
- array[rnd] = temp;
- }
- }
- return array;
- }
- void SelectionSort(int[] array)
- {
- Random rand = new Random();
- int temp, smallest;
- int n = array.Length;
- int index = 0;
- Console.WriteLine("\nSelection sort visualization");
- PrintLines(array);
- while (!IsSorted(array))
- {
- for (int i = 0; i < n; i++)
- {
- smallest = i;
- for (int j = i + 1; j < n; j++)
- {
- if (array[j] < array[smallest])
- {
- smallest = j;
- index++;
- }
- }
- RemoveLines(array.Length, Console.CursorTop);
- PrintLines(array);
- temp = array[smallest];
- array[smallest] = array[i];
- array[i] = temp;
- }
- }
- Console.WriteLine("\nAfter {0} switches, lines are sorted", index);
- }
- void PrintLines(int[] lengths)
- {
- for (int i = 0; i < lengths.Length; i++)
- {
- Console.WriteLine();
- Console.WriteLine(" " + lengths[i] + " " + new String('█', lengths[i] * 6));
- }
- }
- void RemoveLines(int num, int endPos)
- {
- Thread.Sleep(200);
- for (int i = 0; i < num; i++)
- {
- Console.SetCursorPosition(0, endPos - (i * 2) - 1);
- Console.WriteLine(new String(' ', 100));
- }
- Console.SetCursorPosition(0, endPos - (num * 2));
- }
- bool IsSorted(int[] lengths)
- {
- for (int i = 0; i < lengths.Length - 1; i++)
- {
- if (lengths[i] > lengths[i + 1])
- {
- return false;
- }
- }
- return true;
- }
- void VisualBubbleSort(int[] array)
- {
- Random rand = new Random();
- int temp;
- int index = 0;
- Console.WriteLine("\nBubble sort visualization");
- PrintLines(array);
- while (!IsSorted(array))
- {
- for (int i = 0; i < array.Length - 1; i++)
- {
- if (array[i] > array[i + 1])
- {
- temp = array[i];
- array[i] = array[i + 1];
- array[i + 1] = temp;
- index++;
- RemoveLines(array.Length, Console.CursorTop);
- PrintLines(array);
- }
- }
- }
- Console.WriteLine("\nAfter {0} switches, lines are sorted", index);
- }
- void VisualInsertionSort(int[] array)
- {
- Random rand = new Random();
- int n = array.Length, i, j, val, flag;
- int index = 0;
- Console.WriteLine("\nInsertion sort visualization");
- PrintLines(array);
- for (i = 1; i < n; i++)
- {
- val = array[i];
- flag = 0;
- for (j = i - 1; j >= 0 && flag != 1;)
- {
- if (val < array[j])
- {
- array[j + 1] = array[j];
- j--;
- array[j + 1] = val;
- index++;
- RemoveLines(array.Length, Console.CursorTop);
- PrintLines(array);
- }
- else flag = 1;
- }
- }
- Console.WriteLine("\nAfter {0} switches, lines are sorted", index);
- }
- void BogoSort(int[] array)
- {
- int index = 0;
- Console.WriteLine("\nBogo sort visualization");
- PrintLines(array);
- while (!IsSorted(array))
- {
- RemoveLines(array.Length, Console.CursorTop);
- PrintLines(array);
- index++;
- array = ShuffleArray(array);
- }
- RemoveLines(array.Length, Console.CursorTop);
- PrintLines(array);
- Console.WriteLine("\nAfter {0} switches, lines are sorted", index);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement