# Algorithm Search (Linear Search)

Page 1 of 1

## 5 Replies - 9181 Views - Last Post: 17 April 2013 - 04:17 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=318057&amp;s=125b64a6885cf91152ca20b67d36d6e0&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 chan 06

Reputation: 2
• Posts: 67
• Joined: 07-October 12

# Algorithm Search (Linear Search)

Posted 09 April 2013 - 06:25 AM

I got a question on about creating this function, where I use two types of algorithm. The first algorithm would sort the list in a chronological order afterwards it's goes to the function to do a linear search. The problem is, I am getting a syntax error and have no idea what should I do next step.

I added three arguments to take my parameter: list, the length of the list and what you are searching in the list.

```def LinearSearch(array, targetNumber):
for i in range (0,5):
array[i] == targetNumber:
print("The number %d does exist in the list." %(targetNumber))
array[i] != targetNumber:
print("The number %d does not exist in the list." %(targetNumber))

import random
random_number = []
for l in range (0,5):
random_number.append(random.randint(1,100))

# Algorithm Sort
for i in range(0, 5):
for j in range (5-1, -1, -1):
if random_number[j] < random_number[j-1]:
new_value = random_number[j-1]
random_number[j-1] = random_number[j]
random_number[j] = new_value
if j == 1:
break
```

Here is the code, very inefficient and still working own it.

Is This A Good Question/Topic? 0

## Replies To: Algorithm Search (Linear Search)

### #2 jon.kiparsky

• Beginner

Reputation: 11089
• Posts: 18,963
• Joined: 19-March 11

## Re: Algorithm Search (Linear Search)

Posted 09 April 2013 - 07:01 AM

What is the error you're getting?

### #3 Mekire

Reputation: 118
• Posts: 216
• Joined: 11-January 13

## Re: Algorithm Search (Linear Search)

Posted 09 April 2013 - 07:12 AM

You forgot your ifs in your linear_search function (however that function isn't quite right either).

(Also I don't think "chronological" is the word you are looking for with regards to your sort.)

-Mek

### #4 chan 06

Reputation: 2
• Posts: 67
• Joined: 07-October 12

## Re: Algorithm Search (Linear Search)

Posted 09 April 2013 - 05:57 PM

Ah thank you, and also another problem besides the if statements is that when I create my array called "random_number" and goes through the LinearSearch function, it does not go through my Array.

### #5 Mekire

Reputation: 118
• Posts: 216
• Joined: 11-January 13

## Re: Algorithm Search (Linear Search)

Posted 09 April 2013 - 07:24 PM

Well in your original post you didn't show how you are calling your linear_search function, so I can't really comment on that (and if you never called it, then there is your problem).

Also, you really need to put that bubble-sort in a function.

-Mek

### #6 chan 06

Reputation: 2
• Posts: 67
• Joined: 07-October 12

## Re: Algorithm Search (Linear Search)

Posted 17 April 2013 - 04:17 AM

Mekire, on 09 April 2013 - 07:24 PM, said:

Well in your original post you didn't show how you are calling your linear_search function, so I can't really comment on that (and if you never called it, then there is your problem).

Also, you really need to put that bubble-sort in a function.

-Mek

Yes, I didn't call the bubble search function because, I was going to use on my binary search program. In addition the program was running perfectly fine, besides calling out function and adding an if-statement.

Overall thanks for everyone help, I appreciate it.