Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 136,109 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,707 people online right now. Registration is fast and FREE... Join Now!




Binary Search

 
Reply to this topicStart new topic

Binary Search, I'm more of a linear girl... :(

kelliethile
5 Nov, 2007 - 01:47 PM
Post #1

New D.I.C Head
*

Joined: 22 Oct, 2007
Posts: 14


My Contributions
Write a test program implemented in the main() method that, given the preinitilized array of 20 integers { -17, -13, -9, -5, -3, -3, -1, 0, 0, 2, 4, 5, 7, 8, 11, 14, 14, 15, 18, 20 }, displays the index where a user-specified number appears in the array, followed by the number of comparisons performed. (Hint: use a variable to count the actual number of comparisons performed).

Program should says: Element 5 was found at position __(index) after ___(how many tries) comparisons

CODE

/*
* File: Program.cs
* Program: TestSeach
* Author: KTL
*/


using System;
using System.Collections.Generic;
using System.Text;

namespace TestSearch
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] Arr = { -17, -13, -9, -5, -3, -3, -1, 0, 0, 2, 4, 5, 7, 8, 11, 14, 14, 15, 18, 20};
            int index;
            int elem = 5;
            index = Search.Linear( Arr, elem); // i or -1
            if (index == -1)
                Console.Out.WriteLine("Element" + elem + "was not found!" );
            else
            Console.Out.WriteLine("Element " + elem + " was found at position " + index + "after" + Search.Count + "comparisons.");
        }
    }
}


Added a class: Search.cs
CODE

/*
* File: Program.cs
* Program: TestSeach
* Author: KTL
*/


using System;
using System.Collections.Generic;
using System.Text;

namespace TestSearch
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] Arr = { -17, -13, -9, -5, -3, -3, -1, 0, 0, 2, 4, 5, 7, 8, 11, 14, 14, 15, 18, 20};
            int index;
            int elem = 5;
            index = Search.Linear( Arr, elem); // i or -1
            if (index == -1)
                Console.Out.WriteLine("Element" + elem + "was not found!" );
            else
            Console.Out.WriteLine("Element " + elem + " was found at position " + index + "after" + Search.Count + "comparisons.");
        }
    }
}


How do I find how many times it took to find the element 5 in a binary search??? HELP!
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Binary Search
5 Nov, 2007 - 02:01 PM
Post #2

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 6,926



Thanked: 42 times
Dream Kudos: 500
Expert In: C#, VB.NET, Java

My Contributions
More than likely all you need to do is include a counter in each iteration of your loop. But you did not include the code for you Search class, so I am not sure how you are implementing it.

Can you post the code for your Search class?
User is online!Profile CardPM
+Quote Post

kelliethile
RE: Binary Search
7 Nov, 2007 - 06:18 AM
Post #3

New D.I.C Head
*

Joined: 22 Oct, 2007
Posts: 14


My Contributions
how do you edit this thing?


sorry, i copied the codes twice.
it's actually like this

Program.cs (main class)
CODE

/*
* File: Program.cs
* Program: TestSeach
* Author: KTL
*/


using System;
using System.Collections.Generic;
using System.Text;

namespace TestSearch
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] Arr = { -17, -13, -9, -5, -3, -3, -1, 0, 0, 2, 4, 5, 7, 8, 11, 14, 14, 15, 18, 20};
            int index;
            int elem = 5;
            index = Search.Linear( Arr, elem); // i or -1
            if (index == -1)
                Console.Out.WriteLine("Element" + elem + "was not found!" );
            else
            Console.Out.WriteLine("Element " + elem + " was found at position " + index + "after" + Search.Count + "comparisons.");
        }
    }
}


i added a new class, SEARCH.CS
CODE

/*
* File: Search.cs
* Program: TestSeach
* Author: KTL
*
*/
using System;
using System.Collections.Generic;
using System.Text;

namespace TestSearch
{
    class Search
    {
        public static int count;

        public static int Linear(int[] A, int elem)
        {
         //look for elem in array A, return index if found, -1 otherwise
         //Loop i from 0 to A.length - 1
         //if elem == A[i]
         // return i;
         // return -1; // if elem was not found in A

            count = 0;
            for(int i=0; i<A.Length-1; i++)
            {
                count++;
                if (elem == A[i])
                    return i;
            }
            return -1;

            }   //Linear()

        public static int Binary(int[] A, int elem)
            {
            //
            }

    }//class Search
}


How do I find how many times it took to find the element 5 in a binary search??? HELP!
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Binary Search
7 Nov, 2007 - 06:42 AM
Post #4

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 8,983



Thanked: 125 times
Dream Kudos: 8600
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
Well you have a variable count in your Search class, I would make that a variable for a readonly property in your Search class instead of having it as a static variable

CODE

private int _count;

Public int Count
{
     get { return _count; }
}


Doing it that way, the following line will work because you're already setting its value in your Search class

CODE

Console.Out.WriteLine("Element " + elem + " was found at position " + index + "after" + Search.Count + "comparisons.");

User is online!Profile CardPM
+Quote Post

Jayman
RE: Binary Search
7 Nov, 2007 - 10:31 AM
Post #5

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 6,926



Thanked: 42 times
Dream Kudos: 500
Expert In: C#, VB.NET, Java

My Contributions
QUOTE(kelliethile @ 7 Nov, 2007 - 06:18 AM) *

how do you edit this thing?

On the bottom right of each post you will see an EDIT button. Click it and you can edit your post.
User is online!Profile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/1/08 09:30PM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month