Advertisement
valkata

02.Binary Search

Jul 5th, 2017
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.03 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 binarySearch
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             int[] inputArray = Console.ReadLine().Split().Select(int.Parse).ToArray();
  14.             int specialNumber = int.Parse(Console.ReadLine());
  15.  
  16.             LinearSearch(inputArray, specialNumber);
  17.             bool swapped = false;
  18.  
  19.             do
  20.             {
  21.                 swapped = false;
  22.                 for (int i = 0; i < inputArray.Length - 1; i++)
  23.                 {
  24.                     if (inputArray[i] > inputArray[i + 1])
  25.                     {
  26.                         Swap(ref inputArray, ref inputArray[i], ref inputArray[i + 1]);
  27.                         swapped = true;
  28.                     }
  29.                 }
  30.             } while (swapped);
  31.  
  32.             BinarySearch(inputArray, specialNumber);
  33.  
  34.         }
  35.  
  36.         static void LinearSearch(int[] arr, int number)
  37.         {
  38.             int iterations = 0;
  39.             bool isNumberFound = true;
  40.             for (int i = 0; i < arr.Length; i++)
  41.             {
  42.                 if (arr[i] == number)
  43.                 {
  44.                     iterations++;
  45.                     Console.WriteLine("Yes");
  46.                     isNumberFound = true;
  47.                     break;
  48.                 }
  49.                 else if (arr.Length - 1 != number)
  50.                 {
  51.                     isNumberFound = false;
  52.  
  53.                 }
  54.                 iterations++;
  55.             }
  56.             if (!isNumberFound)
  57.             {
  58.                 Console.WriteLine("No");
  59.             }
  60.             Console.WriteLine($"Linear search made {iterations} iterations");
  61.         }
  62.  
  63.         static void BinarySearch(int[] arr, int number)
  64.         {
  65.  
  66.             int iterations = 0;
  67.  
  68.             int low = 0;
  69.             int mid;
  70.             int high = arr.Length - 1;
  71.             int mid1 = 0;
  72.             for (int i = 0; i <arr.Length; i ++)
  73.             {
  74.                
  75.                 mid = low + (high - low) / 2;
  76.                 if(mid == mid1)
  77.                 {
  78.                     iterations--;
  79.                     break;
  80.                 }
  81.                 if (arr[mid] < number)
  82.                 {
  83.                     low = mid + 1;
  84.                    
  85.                     iterations++;
  86.                 }
  87.                 else if (arr[mid] > number)
  88.                 {
  89.                     high = mid - 1;
  90.                     iterations++;
  91.                 }
  92.                 else if (arr[mid] == number)
  93.                 {
  94.                     iterations++;
  95.                     break;
  96.                 }
  97.                 mid1 = mid;
  98.             }
  99.            
  100.             Console.WriteLine($"Binary search made {iterations} iterations");
  101.         }
  102.  
  103.  
  104.  
  105.         static void Swap(ref int[] arr, ref int num1, ref int num2)
  106.         {
  107.             int temporary = num1;
  108.             num1 = num2;
  109.             num2 = temporary;
  110.         }
  111.     }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement