Advertisement
haquaa

Untitled

May 26th, 2016
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.55 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 _2015_C_sharp_Exam
  8. {
  9.     class Program
  10.     {
  11.         static void SortArray(int[] arr)
  12.         {
  13.             for (int i = 0; i < arr.Length; i++)
  14.             {
  15.                 for (int y = 0; y < arr.Length - 1; y++)
  16.                 {
  17.                     if (arr[y] > arr[y + 1])
  18.                     {
  19.                         int temp = arr[y];
  20.                         arr[y] = arr[y + 1];
  21.                         arr[y + 1] = temp;
  22.                     }
  23.                 }
  24.             }
  25.         }
  26.  
  27.         static int BinarySearch(int[] arr, int start, int end, int value)
  28.         {
  29.             while (start <= end)
  30.             {
  31.                 int mid = (start + end) / 2;
  32.                 if (arr[mid] == value)
  33.                 {
  34.                     return mid;
  35.                 }
  36.                 else if (value < arr[mid])
  37.                 {
  38.                     end = mid - 1;
  39.                 }
  40.                 else
  41.                 {
  42.                     start = mid + 1;
  43.                 }
  44.             }
  45.             return -1;
  46.         }
  47.  
  48.         static void Main(string[] args)
  49.         {
  50.             //Question 3 - a
  51.  
  52.             Console.Write("Enter the size of the array : ");
  53.  
  54.             int Size = int.Parse(Console.ReadLine());
  55.  
  56.             int[] arr = new int[Size];
  57.  
  58.             Console.WriteLine("Enter the array elements : ");
  59.  
  60.             for (int i = 0; i < Size; i++)
  61.             {
  62.                 arr[i] = int.Parse(Console.ReadLine());
  63.             }
  64.  
  65.             Console.Write("Originial array : ");
  66.  
  67.             for (int i = 0; i < Size; i++)
  68.             {
  69.                 Console.Write(arr[i] + " ");
  70.             }
  71.  
  72.             SortArray(arr); //bubble sort
  73.  
  74.             Console.WriteLine(); //newline
  75.  
  76.             Console.Write("Sorted array : ");
  77.  
  78.             for (int i = 0; i < Size; i++)
  79.             {
  80.                 Console.Write(arr[i] + " ");
  81.             }
  82.  
  83.             Console.WriteLine();
  84.  
  85.             Console.Write("Enter the value you want to search for in the array : ");
  86.  
  87.             int SearchValue = int.Parse(Console.ReadLine());
  88.  
  89.             int position = BinarySearch(arr, 0, Size - 1, SearchValue);
  90.  
  91.             if (position == -1)
  92.             {
  93.                 Console.WriteLine("Value not found");
  94.             }
  95.             else
  96.             {
  97.                 Console.WriteLine("Found value at element " + position);
  98.             }
  99.         }
  100.     }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement