Advertisement
vkv1986

SearchAlgorithm

Jan 23rd, 2013
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.69 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. class SearchAlgorithm
  5. {
  6.     static int[] GenerateArray(int number)
  7.     {
  8.         Console.WriteLine("Enter the number of elements in the array");
  9.         int elements = int.Parse(Console.ReadLine());
  10.  
  11.         Console.WriteLine("Enter the min value");
  12.         int downLimit = int.Parse(Console.ReadLine());
  13.  
  14.         Console.WriteLine("Enter the max value");
  15.         int upperLimit = int.Parse(Console.ReadLine());
  16.  
  17.         int[] array = new int[elements];
  18.         Random randomGenerator = new Random();
  19.  
  20.         for (int position = 0; position < elements; position++)
  21.         {
  22.             array[position] = randomGenerator.Next(downLimit + position, upperLimit);
  23.         }
  24.  
  25.         for (int position = 0; position < elements; position++)
  26.         {
  27.             Console.Write("{0} ", array[position]);
  28.         }
  29.  
  30.         Console.WriteLine();
  31.         return array;
  32.     }
  33.  
  34.     static void LinearSearch(int[] array, int number)
  35.     {
  36.         int linearIterations = 0;
  37.         for (int position = 0; position < array.Length; position++)
  38.         {
  39.             linearIterations++;
  40.             if (array[position] == number)
  41.             {
  42.                 Console.WriteLine("{0} is the {1} element of the array", number, position + 1);
  43.                 Console.WriteLine("We did {0} iteration", linearIterations);
  44.                 break;
  45.             }
  46.             if ((position == (array.Length - 1)) && (array[position] != number))
  47.             {
  48.                 Console.WriteLine("There is no {0} in the array", number);
  49.                 Console.WriteLine("We did {0} iteration", linearIterations);
  50.             }
  51.         }
  52.     }
  53.  
  54.     static void BinarySearch(int[] array, int number)
  55.     {
  56.         Array.Sort(array);
  57.         int elements = array.Length;
  58.         int binaryIterations = 0;
  59.         bool noNumber = true;
  60.         int firstElement = 0;
  61.         int endElement = elements - 1;
  62.         int middleElement = elements / 2;
  63.  
  64.         while (elements > 0)
  65.         {
  66.             binaryIterations++;
  67.             if (number == array[middleElement])
  68.             {
  69.                 Console.WriteLine("{0} is on {1} position in the sorted array (from 0)", number, middleElement);
  70.                 Console.WriteLine("We did {0} iteration", binaryIterations);
  71.                 noNumber = false;
  72.                 break;
  73.             }
  74.             else if (number < array[middleElement])
  75.             {
  76.                 if (firstElement != middleElement)
  77.                 {
  78.                     endElement = middleElement - 1;
  79.                     elements = middleElement - firstElement;
  80.                     middleElement = firstElement + elements / 2;
  81.                 }
  82.                 else
  83.                 {
  84.                     elements = 0;
  85.                 }
  86.             }
  87.             else
  88.             {
  89.                 if (firstElement != middleElement)
  90.                 {
  91.                     firstElement = middleElement + 1;
  92.                     elements = endElement - middleElement;
  93.                     middleElement = firstElement + elements / 2;
  94.                 }
  95.                 else
  96.                 {
  97.                     elements = 0;
  98.                 }
  99.             }
  100.         }
  101.         if (noNumber)
  102.         {
  103.             Console.WriteLine("There is no {0} in the array", number);
  104.             Console.WriteLine("We did {0} iteration", binaryIterations);
  105.         }
  106.     }
  107.  
  108.     static void Main()
  109.     {
  110.         Console.WriteLine("Enter the number");
  111.         int number = int.Parse(Console.ReadLine());
  112.  
  113.         int[] array = GenerateArray(number);
  114.  
  115.         LinearSearch(array, number);
  116.  
  117.         BinarySearch(array, number);
  118.     }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement