binary searching using recursion function

Binary searching using Recursion method


using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace search_a_number_in_an_aray { class Program { public static void Main(string[] args) { int[] num = new int[] { 2, 6, 11, 8,66, 3,45,46,78,77 }; int found, searchItem; int first = 0, last = num.Length-1; // sort function bubblesort(num); //Output function output(num); Console.WriteLine(); // input the search item Console.WriteLine("Enter the number :"); searchItem = int.Parse(Console.ReadLine()); // search function found = search(num, searchItem,first,last); // If found if (found == -1) { Console.WriteLine("Element is not present"); } Console.WriteLine("The number {0} is found in {1} postition", searchItem, found); Console.ReadKey(); } // Bubble sort function static public void bubblesort(int[] num) { int i, temp = 0; bool swapMade = true; while (swapMade == true) { swapMade = false; for (i = 0; i < num.Length - 1; i++) { if (num[i] > num[i + 1]) { temp = num[i]; num[i] = num[i + 1]; num[i + 1] = temp; swapMade = true; } } } } // Output function static public void output(int[] num) { int i; for (i = 0; i < num.Length; i++) { Console.WriteLine(num[i]); } } // binary search function static int search(int[] num, int n,int first,int last) { // while (first <= last) //{ int mid = (first + last) / 2; if (num[mid] == n) return mid; else if (n < num[mid]) return search(num,n,first,mid-1); else if (n > num[mid]) return search(num,n,first=mid+1,last); else return -1; // } }// function end } }

Comments

Popular posts from this blog