# Linear Search Algorithm

Page 1 of 1

## 6 Replies - 13620 Views - Last Post: 14 November 2008 - 09:36 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=71833&amp;s=3a559152a94e501aecdf0e6ad223ec9b&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 ThuroRamming

Reputation: 0
• 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
{
}
}

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

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

Reputation: 8378
• Posts: 31,956
• 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())

### #3 Locke

• Sarcasm Extraordinaire!

Reputation: 550
• Posts: 5,624
• 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

### #4 pbl

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

Reputation: 8378
• Posts: 31,956
• 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;

Locke37, 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

### #5 stauffski

Reputation: 27
• Posts: 216
• 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
{
}
}

public static void main(String args[])
{
LinearSearch game = new LinearSearch();
game.play();
}

}

Good Luck!

### #6 pbl

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

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: Linear Search Algorithm

Posted 14 November 2008 - 05:19 AM

pbl, 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

### #7 Locke

• Sarcasm Extraordinaire!

Reputation: 550
• Posts: 5,624
• Joined: 20-March 08

## Re: Linear Search Algorithm

Posted 14 November 2008 - 09:36 AM

pbl, on 14 Nov, 2008 - 04:19 AM, said:

pbl, 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

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

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