0

I have searched the questions on here and found that some questions are very similar to my question but not exactly what I'm after. Apologies if this has indeed been asked for before.

I would like to be able to time the bubble sort and insertion sort options in my programme and have the time it took to sort printed below the sorted array of random integers. I have tried a couple of things but to be honest I don't even know where to start with this part of the challenge. Any help you could give me would be so appreciated

import random
import time

listnum = int(input("What size list would you like? "))
randomlist = random.sample(range(0, 5000), listnum)
print(randomlist)
sort_i = input("Would you like to sort your list Y or N? ")

if sort_i == "Y":
    search_i = input("Bubble sort or Insertion sort? ")
    if search_i == "Bubble sort":

        for i in range(len(randomlist) - 1, 0, -1):
            for j in range(i):
                if randomlist[j] > randomlist[j + 1]:
                    randomlist[j], randomlist[j + 1] = randomlist[j + 1], randomlist[j]


    if search_i == "Insertion sort":
        for i in range(1, len(randomlist)):
            key = randomlist[i]
            j = i - 1
            while j >= 0 and key < randomlist[j]:
                randomlist[j + 1] = randomlist[j]
                j -= 1
            randomlist[j + 1] = key
    print(randomlist)
elif sort_i == "N":

    lin_search_num = int(input("What number would you like to linear search? "))
    for s in range(len(randomlist)):
        if randomlist[s] == lin_search_num:
            print("Your number is at index " + str(s + 1))
James JPN
  • 61
  • 4

2 Answers2

2

May I suggest this?

import time

start = time.time() # in seconds since epoch
# your code
end = time.time()

print("{:.3f}".format(end - start), "seconds")

Actually, it would be better to use time.process_time() instead of time.time().

Orius
  • 1,093
  • 5
  • 11
1

Would this work? I'm using time.time() which returns the number of seconds since the epoch.

import random
import time

listnum = int(input("What size list would you like? "))
randomlist = random.sample(range(0, 5000), listnum)
print(randomlist)
sort_i = input("Would you like to sort your list Y or N? ")

if sort_i == "Y":
    search_i = input("Bubble sort or Insertion sort? ")
    st = time.time() # Start time in seconds
    if search_i == "Bubble sort":
        for i in range(len(randomlist) - 1, 0, -1):
            for j in range(i):
                if randomlist[j] > randomlist[j + 1]:
                    randomlist[j], randomlist[j + 1] = randomlist[j + 1], randomlist[j]

    if search_i == "Insertion sort":
        for i in range(1, len(randomlist)):
            key = randomlist[i]
            j = i - 1
            while j >= 0 and key < randomlist[j]:
                randomlist[j + 1] = randomlist[j]
                j -= 1
            randomlist[j + 1] = key
    et = time.time() # End time in seconds
    print(randomlist)
    print(f"Time taken to sort in seconds: {et - st}")
elif sort_i == "N":

    lin_search_num = int(input("What number would you like to linear search? "))
    st = time.time()
    for s in range(len(randomlist)):
        if randomlist[s] == lin_search_num:
            print("Your number is at index " + str(s + 1))
            print(f"Took {time.time() - st} seconds.")
Codeman
  • 477
  • 3
  • 13
  • This is great! Thanks so much for this! – James JPN Feb 12 '22 at 06:48
  • Thanks so much for adding the linear search too! – James JPN Feb 12 '22 at 06:53
  • 1
    You're welcome! I chose to put the `time.time()` statements after the `input()` statements because otherwise it would also time the time you took to input. if You want to print the start and end time too, use `time.strftime("%H:%M:%S")`. – Codeman Feb 12 '22 at 06:55
  • One thing though and doesn't really matter too much but when it displays the time it displays it like this: Time taken to sort in seconds: -4.9905641078948975 Should it be displayed in minus seconds? Or is that unavoidable? – James JPN Feb 12 '22 at 06:58
  • 1
    Hold up I'm an idiot. It should have been `et - st`. I've corrected it! (I had it as start time - end time, instead of end time - start time). – Codeman Feb 12 '22 at 07:19
  • You are no idiot sir! I should have caught that myself. Can't thank you enough for your help today! – James JPN Feb 12 '22 at 07:22