Binary Search

I'm more of a linear girl... :(

Page 1 of 1

4 Replies - 7030 Views - Last Post: 07 November 2007 - 11:31 AM Rate Topic: -----

#1 kelliethile  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 22-October 07

Binary Search

Posted 05 November 2007 - 02:47 PM

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

/*
 * 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
/*
 * 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!

Is This A Good Question/Topic? 0
  • +

Replies To: Binary Search

#2 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Binary Search

Posted 05 November 2007 - 03:01 PM

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?
Was This Post Helpful? 0
  • +
  • -

#3 kelliethile  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 22-October 07

Re: Binary Search

Posted 07 November 2007 - 07:18 AM

how do you edit this thing?


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

Program.cs (main class)
/*
 * 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
/*
 * 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!
Was This Post Helpful? 0
  • +
  • -

#4 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1639
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Binary Search

Posted 07 November 2007 - 07:42 AM

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

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

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


Was This Post Helpful? 0
  • +
  • -

#5 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Binary Search

Posted 07 November 2007 - 11:31 AM

View Postkelliethile, on 7 Nov, 2007 - 06:18 AM, said:

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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1