Advertisement
grubcho

Binary search

Jul 3rd, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.82 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Binary_search
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             List<int> list = Console.ReadLine().Split(' ').Select(int.Parse).ToList();
  14.             int num = int.Parse(Console.ReadLine());
  15.             int cnt = 0;
  16.  
  17.             if (LinearSearch(list, num, ref cnt))
  18.             {
  19.                 Console.WriteLine("Yes");
  20.                 Console.WriteLine($"Linear search made {cnt} iterations");
  21.             }
  22.             else
  23.             {
  24.                 Console.WriteLine("No");
  25.                 Console.WriteLine($"Linear search made {cnt} iterations");
  26.             }
  27.  
  28.             int binaryCount = BinarySearch(list, num);
  29.             Console.WriteLine($"Binary search made {binaryCount} iterations");            
  30.         }
  31.         static bool LinearSearch(List<int> nums, int num, ref int count)
  32.         {
  33.            
  34.             for (int i = 0; i < nums.Count; i++)
  35.             {
  36.                 count++;
  37.                 if (nums[i] == num)
  38.                 {
  39.                     return true;
  40.                 }              
  41.             }
  42.             return false;
  43.         }
  44.         static int BinarySearch(List<int> nums, int num)
  45.         {
  46.             InsertionSort(nums);
  47.             //nums.Sort();
  48.             int low = 0;
  49.             int high = nums.Count - 1;
  50.             int midpoint = 0;
  51.             int count = 0;
  52.  
  53.             while (low <= high)
  54.             {
  55.                 midpoint = low + (high - low) / 2;
  56.                 count++;
  57.                 if (num == nums[midpoint])
  58.                 {
  59.                     return count;
  60.                 }
  61.                 else if (num < nums[midpoint])
  62.                 {
  63.                     high = midpoint - 1;
  64.                 }
  65.                    
  66.                 else if (num > nums[midpoint])
  67.                 {
  68.                     low = midpoint + 1;
  69.                 }
  70.                    
  71.             }
  72.             return count;
  73.         }
  74.         static void InsertionSort(List<int> nums)
  75.         {
  76.             for (int index1 = 0; index1 < nums.Count; index1++)
  77.             {
  78.                 for (int index2 = index1; index2 > 0; index2--)
  79.                 {
  80.                     if (nums[index2 - 1] > nums[index2])
  81.                     {
  82.                         Swap(nums, index2 - 1, index2);
  83.                     }
  84.                     else
  85.                     {
  86.                         break;
  87.                     }
  88.                 }
  89.             }
  90.         }
  91.         static void Swap(List<int> nums, int index1, int index2)
  92.         {
  93.             int temp = nums[index1];
  94.             nums[index1] = nums[index2];
  95.             nums[index2] = temp;
  96.         }
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement