Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace SorterRacela161618
- {
- class MainClass
- {
- public static void Main(string[] args)
- {
- bool start = true;
- while (start)
- {
- start = false;
- Console.Write("Input Numbers: ");
- string numbers = Console.ReadLine();
- string[] splitted = numbers.Split(' ');
- int[] nums = new int[splitted.Length];
- for (int i = 0; i < splitted.Length; i++)
- {
- nums[i] = int.Parse(splitted[i]);
- }
- Console.Write("Current Array: " + nums[0]);
- for (int i = 1; i < splitted.Length; i++)
- {
- Console.Write(" " + nums[i]);
- }
- Console.WriteLine();
- Console.WriteLine("1 for Binsertion Sort");
- Console.WriteLine("2 for Bubbly Sort");
- Console.Write("Your choice: ");
- int choice = int.Parse(Console.ReadLine());
- if (choice == 1)
- {
- MainClass mc = new MainClass();
- mc.BinsertionSort(nums);
- }
- else if (choice == 2)
- {
- MainClass mc = new MainClass();
- mc.BubblySort(nums);
- }
- else
- {
- Console.WriteLine("Invalid Input! Enter 1 or 2 only.");
- Console.WriteLine();
- start = true;
- }
- }
- }
- public void BinsertionSort(int[] list)
- {
- for (int i = 1; i < list.Length; i++)
- {
- int ins = list[i];
- int p = BinarySearchNumber(list, i - 1, ins);
- for (int j = i - 1; j >= p; j--)
- {
- list[j + 1] = list[j];
- }
- list[p] = ins;
- Console.Write(list[0]);
- for (int a = 1; a < list.Length; a++)
- {
- Console.Write(" " + list[a]);
- }
- Console.WriteLine();
- }
- }
- public int BinarySearch(int[] list, int abc, int value)
- {
- int low = 0;
- int high = abc;
- while (low <= high)
- {
- int mid = (low + high) / 2;
- if (high == low)
- {
- return low;
- }
- else if (list[mid] < value)
- {
- low = mid + 1;
- }
- else if (list[mid] > value)
- {
- high = mid;
- }
- else
- return mid;
- }
- return -1;
- }
- public int BinarySearchNumber(int[] list, int high, int ins)
- {
- int low = 0;
- while (low <= high)
- {
- int mid = (low + high) / 2;
- if (list[mid] < ins)
- {
- low = mid + 1;
- }
- else
- {
- high = mid - 1;
- }
- }
- return low;
- }
- public void BubblySort(int[] list)
- {
- for (int p = 1; p <= list.Length / 2; p++)
- {
- for (int i = p - 1; i <= (list.Length - 1) - p; i++)
- {
- if (list[i] > list[i + 1])
- {
- int temp = list[i];
- list[i] = list[i + 1];
- list[i + 1] = temp;
- for (int a = 0; a < list.Length; a++)
- {
- Console.Write(list[a] + " ");
- }
- Console.WriteLine();
- }
- }
- for (int i = list.Length - p - 1; i >= p; i--)
- {
- if (list[i] < list[i - 1])
- {
- int temp = list[i];
- list[i] = list[i - 1];
- list[i - 1] = temp;
- for (int a = 0; a < list.Length; a++)
- {
- Console.Write(list[a] + " ");
- }
- Console.WriteLine();
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement