Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Time sorting in python

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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: {st - et}")
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.")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading