Linear Search Algorithm

just some debugging

Page 1 of 1

6 Replies - 12143 Views - Last Post: 14 November 2008 - 09:36 AM Rate Topic: -----

#1 ThuroRamming  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 09-October 08

Linear Search Algorithm

Posted 13 November 2008 - 10:49 PM

Here is what I have for a linear search algorithm, but i'm getting a few errors here and there and i can't figure out how to finish it off. Can one of you fix it and let me know if there is a better way to set up my array?


public class LinearSearch
{
	
	public int target;
	public int[] data = new int[20];
	
	public void linearSearch()
	{
		target = 0;
		
		data[0] = 1;
		data[1] = 2;
		data[2] = 3;
		data[3] = 4;
		data[4] = 5;
		data[5] = 6;
		data[6] = 7;
		data[7] = 8;
		data[8] = 9;
		data[9] = 10;
		data[10] = 11;
		data[11] = 12;
		data[12] = 13;
		data[13] = 14;
		data[14] = 15;
		data[15] = 16;
		data[16] = 17;
		data[17] = 18;
		data[18] = 19;
		data[19] = 20;
		
	}

	public static boolean linearSearch(int[] data, int target)
	{
		for (int i = 0; i < data.length; i++)
		{
			if (data[i] == target)
			{
				return true;
			}
		}
		return false;
	}
	   
	public void play()
	{
		if (linearSearch() = true)
		{
			System.out.println("Integer found");
		}
		else
		{
			System.out.println("Integer not found");
		}
	}
	
	   public static void main(String args[])
	{
		LinearSearch game = new LinearSearch();
		game.play();
	}
	
}



Is This A Good Question/Topic? 0
  • +

Replies To: Linear Search Algorithm

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Linear Search Algorithm

Posted 13 November 2008 - 10:53 PM

if (linearSearch() = true)
should be
if (linearSearch() == true)
or
if (linearSearch())
Was This Post Helpful? 0
  • +
  • -

#3 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 520
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: Linear Search Algorithm

Posted 13 November 2008 - 10:54 PM

Well, to clean up your linearSearch() method...

for (int i = 0; i < data.length; i++)
    data[i] = i + 1;


That will have the same effect as all of those data[...] = #;.

Second, in your play() method, you should probably supply parameters to the linearSearch(int[], int) method to find if the integer is there. If you don't, you should probably be getting an error...

And pbl is right, change it to one of his solutions.

This post has been edited by Locke37: 13 November 2008 - 10:55 PM

Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Linear Search Algorithm

Posted 13 November 2008 - 10:56 PM

and
 data[0] = 1;
		data[1] = 2;
		data[2] = 3;
		data[3] = 4;
		data[4] = 5;
		data[5] = 6;
		data[6] = 7;
		data[7] = 8;
		data[8] = 9;
		data[9] = 10;
		data[10] = 11;
		data[11] = 12;
		data[12] = 13;
		data[13] = 14;
		data[14] = 15;
		data[15] = 16;
		data[16] = 17;
		data[17] = 18;
		data[18] = 19;
		data[19] = 20;


can easily be replaced by
for(int i = 0; i < 20; i++)
   data[i] = i + 1;



View PostLocke37, on 13 Nov, 2008 - 09:54 PM, said:

Well, to clean up your linearSearch() method...

for (int i = 0; i < data.length; i++)
    data[i] = i + 1;


That will have the same effect as all of those data[...] = #;.

Second, in your play() method, you should probably supply parameters to the linearSearch(int[], int) method to find if the integer is there. If you don't, you should probably be getting an error...

And pbl is right, change it to one of his solutions.

oups sorry Locke concurrent replies
OK I'm going to bed it is all yours
have fun holding the fort
Was This Post Helpful? 0
  • +
  • -

#5 stauffski  Icon User is offline

  • D.I.C Head

Reputation: 27
  • View blog
  • Posts: 210
  • Joined: 03-November 07

Re: Linear Search Algorithm

Posted 13 November 2008 - 11:08 PM

The following should fix your code:

public class LinearSearch
{
    
    public int target;
    public int[] data = new int[20];
    
    public LinearSearch()//from "public void linearSearch()" to "public LinearSearch()"
    {
        target = 3;
        
        data[0] = 1;
        data[1] = 2;
        data[2] = 3;
        data[3] = 4;
        data[4] = 5;
        data[5] = 6;
        data[6] = 7;
        data[7] = 8;
        data[8] = 9;
        data[9] = 10;
        data[10] = 11;
        data[11] = 12;
        data[12] = 13;
        data[13] = 14;
        data[14] = 15;
        data[15] = 16;
        data[16] = 17;
        data[17] = 18;
        data[18] = 19;
        data[19] = 20;
        
    }

    public static boolean linearSearch(int[] data, int target)
    {
        for (int i = 0; i < data.length; i++)
        {
            if (data[i] == target)
            {
                return true;
            }
        }
        return false;
    }
      
    public void play()
    {
        if (linearSearch(data, target))//from "linearSearch() =(should be "==") true" to "linearSearch(data, target)"
        {
            System.out.println("Integer found");
        }
        else
        {
            System.out.println("Integer not found");
        }
    }
    
       public static void main(String args[])
    {
        LinearSearch game = new LinearSearch();
        game.play();
    }
    
}



Good Luck!
Was This Post Helpful? 1
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Linear Search Algorithm

Posted 14 November 2008 - 05:19 AM

View Postpbl, on 13 Nov, 2008 - 09:53 PM, said:

if (linearSearch() = true)
should be
if (linearSearch() == true)
or
if (linearSearch())


oups.... and you also have to provide the arguments to your linearSearch() method
Was This Post Helpful? 0
  • +
  • -

#7 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 520
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: Linear Search Algorithm

Posted 14 November 2008 - 09:36 AM

View Postpbl, on 14 Nov, 2008 - 04:19 AM, said:

View Postpbl, on 13 Nov, 2008 - 09:53 PM, said:

if (linearSearch() = true)
should be
if (linearSearch() == true)
or
if (linearSearch())


oups.... and you also have to provide the arguments to your linearSearch() method


:D

I'm getting more perceptive in quickly scanning others' code. YAY.

This post has been edited by Locke37: 14 November 2008 - 09:39 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1