-6

This is a basic exercise. I just don't know hot to implement the timeit module correctly. I keep recieving syntax errors

import timeit
import random
def bubblesort(LAux):
    for i in range(len(LAux)): 
    exchange = False 
    for j in range(len(LAux)-1): 
        if LAux[j] > LAux[j+1]: 
            tmp = LAux[j+1] 
            LAux[j+1] = LAux[j] 
            LAux[j] = tmp 
            exchange = True
    if not exchange: 
        break
return(LAux)

a=int(input("Size of list: "))
lst = [0]*a
for i in range(a):
    lst[i] = random.randint(1,100)
print(bubblesort(lst))
print(timeit.timeit()
  • please provide the syntax errors. – pbuck Aug 20 '17 at 17:10
  • 2
    Lines 5, 12, 13, 14 needs to be indented and you're missing a closing parentheses on the last line; pretty much what the error message tells you. – jpw Aug 20 '17 at 17:13
  • For me it works if you put a `)` behind the last `print()` – Bart Aug 20 '17 at 17:13
  • Please provide syntax error. As i have noticed. Error is "print(timeit.timeit()". You missed second parenthesis ")". – logsv Aug 20 '17 at 17:13
  • 2
    That code isn't indented properly, and it's missing a closing parenthesis `)` at the end. What do you expect `print(timeit.timeit())` to do? You haven't given it anything to perform a timing test on. – PM 2Ring Aug 20 '17 at 17:14
  • Thanks for the quick answers. Actually i want to calculate the execution time of the bubblesort function. I didn't notice that missing parenthesis and I just realized that, somehow, it works if i run the code with it. But according to the python documentation, the module needs 4 arguments. So, any ideas how to use it? or maybe the number it shows me its the time i'm looking for. – Sebastian M. Aug 20 '17 at 17:42
  • BTW, there's a better way to swap things in Python. You don't need that `tmp`, you can just do `LAux[j], LAux[j+1] = LAux[j+1], LAux[j]` – PM 2Ring Aug 20 '17 at 17:58

1 Answers1

1

You seem to have misinterpreted the function of timeit.timeit - the idea is that you tell it what to actually time, and it then times it. Normally, it does the thing you're timing many, many times, so you can get a meaningful average. This means it also needs to provide a 'setup' argument, as the list should presumably be different for each test. Refer to the examples in the documentation if you need - I normally find that they're easy enough to suit to your purpose. I've implemented timeit below:

a=int(input("Size of list: "))

n = 100000

setup = "lst = [random.randrange(100) for _ in range(a)]"
time = timeit.timeit("bubblesort(lst)", setup=setup, globals=globals(), number=n)

print("{} sorts took {}s".format(n, time))

Of course, if you're using IPython you can do something like this:

In [1]: from bubble import bubblesort

In [2]: from random import randrange

In [3]: %timeit bubblesort([randrange(100) for _ in range(50)])
10000 loops, best of 3: 175 µs per loop

Maybe you were expecting timeit() to tell you just how much time has passed. If so, this could be done using the time module, quite simply.

import time

start_time = time.time()
bubblesort(lst)
time_taken = time.time() - start_time
print("One sort took {}s".format(time_taken))

On another note, I'd recommend using a different name for the argument "Laux". As you can see, SO's syntax highlighting thinks it's a class, as normally anything starting with a capital letter is a class. Laux also doesn't particularly tell me as an outsider what it is, although I can infer from the name bubblesort.

Izaak van Dongen
  • 2,450
  • 13
  • 23