Editorial Staff - - C Sharp Tutorial
If you are a c# beginner or want to start learning the c# programming language, then this program will help you to understand the basics of c# programming. In this program, we are going to share C# program to check fixed point in an array using binary search with the output.
Copy the below c# program and execute it in your Microsoft Visual Studio IDE (Integrated Development Environment ). At the end of this program, I have shared the output program.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | using System; class FWM { static int binarySearch(int []arr, int low, int high) { if(high >= low) { // low + (high - low)/2; int mid = (low + high)/2; if(mid == arr[mid]) return mid; if(mid > arr[mid]) return binarySearch(arr, (mid + 1), high); else return binarySearch(arr, low, (mid -1)); } /* Return -1 if there is no Fixed Point */ return -1; } // Driver code public static void Main() { int []arr = {-10, -1, 0, 3 , 10, 11, 30, 50, 100}; int n = arr.Length; Console.Write("Fixed Point is "+ binarySearch(arr,0, n-1)); } } |
Fixed Point is 3
Editorial Staff at FreeWebMentor is a team of professional developers leads by Prem Tiwari View all posts by Editorial Staff