Advertisement
Guest User

binsertion

a guest
Apr 19th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.62 KB | None | 0 0
  1. using System;
  2.  
  3. namespace SorterRacela161618
  4. {
  5.     class MainClass
  6.     {
  7.         public static void Main(string[] args)
  8.         {
  9.             bool start = true;
  10.             while (start)
  11.             {
  12.                 start = false;
  13.                 Console.Write("Input Numbers: ");
  14.                 string numbers = Console.ReadLine();
  15.                 string[] splitted = numbers.Split(' ');
  16.                 int[] nums = new int[splitted.Length];
  17.  
  18.                 for (int i = 0; i < splitted.Length; i++)
  19.                 {
  20.                     nums[i] = int.Parse(splitted[i]);
  21.                 }
  22.  
  23.                 Console.Write("Current Array: " + nums[0]);
  24.                 for (int i = 1; i < splitted.Length; i++)
  25.                 {
  26.                     Console.Write(" " + nums[i]);
  27.                 }
  28.                 Console.WriteLine();
  29.                 Console.WriteLine("1 for Binsertion Sort");
  30.                 Console.WriteLine("2 for Bubbly Sort");
  31.                 Console.Write("Your choice: ");
  32.                 int choice = int.Parse(Console.ReadLine());
  33.                 if (choice == 1)
  34.                 {
  35.                     MainClass mc = new MainClass();
  36.                     mc.BinsertionSort(nums);
  37.                 }
  38.                 else if (choice == 2)
  39.                 {
  40.                     MainClass mc = new MainClass();
  41.                     mc.BubblySort(nums);
  42.                 }
  43.                 else
  44.                 {
  45.                     Console.WriteLine("Invalid Input! Enter 1 or 2 only.");
  46.                     Console.WriteLine();
  47.                     start = true;
  48.                 }
  49.             }
  50.  
  51.         }
  52.  
  53.         public void BinsertionSort(int[] list)
  54.         {
  55.             for (int i = 1; i < list.Length; i++)
  56.             {
  57.                 int ins = list[i];
  58.                 int p = BinarySearchNumber(list, i - 1,  ins);
  59.                 for (int j = i - 1; j >= p; j--)
  60.                 {
  61.                     list[j + 1] = list[j];
  62.                 }
  63.                 list[p] = ins;
  64.                 Console.Write(list[0]);
  65.                 for (int a = 1; a < list.Length; a++)
  66.                 {
  67.                     Console.Write(" " + list[a]);
  68.                 }
  69.                 Console.WriteLine();
  70.             }
  71.  
  72.         }
  73.  
  74.         public int BinarySearch(int[] list, int abc, int value)
  75.         {
  76.             int low = 0;
  77.             int high = abc;
  78.             while (low <= high)
  79.             {
  80.                 int mid = (low + high) / 2;
  81.                 if (high == low)
  82.                 {
  83.                     return low;
  84.                 }
  85.                 else if (list[mid] < value)
  86.                 {
  87.                     low = mid + 1;
  88.                 }
  89.                 else if (list[mid] > value)
  90.                 {
  91.                     high = mid;
  92.                 }
  93.                 else
  94.                     return mid;
  95.             }
  96.             return -1;
  97.         }
  98.  
  99.         public int BinarySearchNumber(int[] list, int high, int ins)
  100.         {
  101.             int low = 0;
  102.             while (low <= high)
  103.             {
  104.                 int mid = (low + high) / 2;
  105.                 if (list[mid] < ins)
  106.                 {
  107.                     low = mid + 1;
  108.                 }
  109.                 else
  110.                 {
  111.                     high = mid - 1;
  112.                 }
  113.             }
  114.             return low;
  115.         }
  116.  
  117.         public void BubblySort(int[] list)
  118.         {
  119.             for (int p = 1; p <= list.Length / 2; p++)
  120.             {
  121.                 for (int i = p - 1; i <= (list.Length - 1) - p; i++)
  122.                 {
  123.                     if (list[i] > list[i + 1])
  124.                     {
  125.                         int temp = list[i];
  126.                         list[i] = list[i + 1];
  127.                         list[i + 1] = temp;
  128.                         for (int a = 0; a < list.Length; a++)
  129.                         {
  130.                             Console.Write(list[a] + " ");
  131.                         }
  132.                         Console.WriteLine();
  133.                     }
  134.                 }
  135.                 for (int i = list.Length - p - 1; i >= p; i--)
  136.                 {
  137.                     if (list[i] < list[i - 1])
  138.                     {
  139.                         int temp = list[i];
  140.                         list[i] = list[i - 1];
  141.                         list[i - 1] = temp;
  142.                         for (int a = 0; a < list.Length; a++)
  143.                         {
  144.                             Console.Write(list[a] + " ");
  145.                         }
  146.                         Console.WriteLine();
  147.                     }
  148.                 }
  149.             }
  150.         }
  151.     }
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement