Home > Software design >  Time sorting in python
Time sorting in python

Time:02-12

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))

CodePudding user response:

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().

CodePudding user response:

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.")
  • Related