5 Replies - 1410 Views - Last Post: 17 April 2013 - 04:17 AM Rate Topic: -----

#1 chan 06  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 63
  • 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  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7562
  • View blog
  • Posts: 12,680
  • Joined: 19-March 11

Re: Algorithm Search (Linear Search)

Posted 09 April 2013 - 07:01 AM

What is the error you're getting?
Was This Post Helpful? 0
  • +
  • -

#3 Mekire  Icon User is offline

  • D.I.C Head

Reputation: 116
  • View blog
  • Posts: 212
  • 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
Was This Post Helpful? 1
  • +
  • -

#4 chan 06  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 63
  • 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.
Was This Post Helpful? 0
  • +
  • -

#5 Mekire  Icon User is offline

  • D.I.C Head

Reputation: 116
  • View blog
  • Posts: 212
  • 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
Was This Post Helpful? 1
  • +
  • -

#6 chan 06  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 63
  • Joined: 07-October 12

Re: Algorithm Search (Linear Search)

Posted 17 April 2013 - 04:17 AM

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

Page 1 of 1