0
using System.IO;
using System;
class Class1
{
    static void Main(string[] args)
    {
        int[] arr = new int[10] { 1,3,2,4,5,7,6,8,10,9};
        int l = 1, r = 10, m = 0,t;

        sort(arr, 10); 
        int i;
        for (i = 0; i < 10; i++)
        {
            Console.Write(arr[i] + "\t");
        }
        Console.WriteLine("Please entert the number");
        t = Convert.ToInt32(Console.ReadLine());

        m = (l + r) / 2;

        while(l>=r)
        {
            if (arr[m] == t)
               Console.WriteLine("value is : " + m);
            if (arr[m] < t)
                l = m + 1;
            if (arr[m] > t)
                r = m - 1;
            else
                Console.WriteLine("Target was found at index  " + arr[m]);
        }
    }

    static void sort(int[] dataset, int n)
    {
        int i, j;
        for (i = 0; i < n; i++)
            for (j = n - 1; j > i; j--)
                if (dataset[j] < dataset[j - 1])
                {
                    int temp = dataset[j];
                    dataset[j] = dataset[j - 1];
                    dataset[j - 1] = temp;
                }
    }
}

I tried running this program. I got output as :

sh-4.3$ mcs *.cs -out:main.exe
sh-4.3$ mono main.exe
1 2 3 4 5 6 7 8 9
10
Please enter the number
5
sh-4.3$

What should I do to get the output of binary search from the sorted array?

5
  • You know you could use Array.Sort instead of writing your own bubble sort implementation. Commented Apr 6, 2016 at 11:46
  • You do realize that l is never >= r. So you never enter the while loop. I think you want while(l<=r) Commented Apr 6, 2016 at 11:47
  • thanks for the info @juharr. but i wanted to try it with bubble sort. is there an error in my code? Commented Apr 6, 2016 at 11:49
  • Also your else in the while loop will never be reached since arr[m] can only be equal to, less than, or greater than t. There are no other possibilities. Commented Apr 6, 2016 at 11:53
  • Also you need to recalculate the m in the loop. Commented Apr 6, 2016 at 11:55

1 Answer 1

1

There are lots of issues with your code

  1. Bad formatting - make it difficult to read
  2. Bad variable names - make it difficult to understand
  3. Variables not declared close to where they are being used
  4. Not using inbuilt routine for sort (assuming your focus is on learning binary search)

Now, logically the code fails, since the while loop for your binary search is flawed. Below is the correct code for your main - I have tried to keep it as similar to your code as possible, so you can understand the issues

static void Main(string[] args) {
    int[] arr = new int[10] { 1, 3, 2, 4, 5, 7, 6, 8, 10, 9 };


    sort(arr, 10);
    int i;
    for (i = 0; i < 10; i++) {
        Console.Write(arr[i] + "\t");
    }

    int t;
    Console.WriteLine("Please entert the number");
    t = Convert.ToInt32(Console.ReadLine());

    bool found = false;
    int l = 0, r = arr.Length - 1, m = 0;
    while (!found && l <= r) {
        m = (l + r) / 2;
        if (arr[m] == t)
            found = true;
        else if (arr[m] < t)
            l = m + 1;
        else if (arr[m] > t)
            r = m - 1;
    }
    if (found)
        Console.WriteLine($"value {t} is located at index {m}");
    else
        Console.WriteLine($"value {t} not found");
}
Sign up to request clarification or add additional context in comments.

3 Comments

thank you for your effort and help. i am a fresher in C# and in learning process... Sorry for the trouble..... Thanks again!
btw. Console.WriteLine($"value {t} is located at index {m}"); What is the necessity of $ and {} in that statement?
It is a new syntax with c# 6. In older versions, you can write that as Console.WriteLine("value {0} is located at index {1}", t, m). Read up on Interpolated strings for more details

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.