0
\$\begingroup\$

This bubble sort code is very slow compared to the original.

import time
import random
def bubble_sort(a):
    #setting my variables
    yes=0
    d=0
    #making a switch
    while yes==0:
        if d>=last_thing-1:
            d=0
        if a[0+d]>a[1+d]:

            #switching the order
            a[0+d],a[1+d]=a[1+d],a[0+d]

        d+=1
        if correct_order(a):
            #turning off the switch
            yes=1

    return(a)
def correct_order(a):
    for i in range(len(a)):
        if i != a[i]:
            return(False)
    return(True)
if __name__ == '__main__':
    #setting your range
    last_thing=1000+1
    #making the random list
    a= list(range(0,last_thing))
    random.shuffle(a)

    #easier setting
    currentsortingmethod=bubble_sort(a)
    #prints your sorted list
    print("--------------------------------------------------------------------------------\n",str(currentsortingmethod).strip('[]').replace(" ", "").replace("'",""))
\$\endgroup\$
  • \$\begingroup\$ Hello and welcome to CodeReview. Your post needs some work. Please reformat it so that your code is all included in a code block. \$\endgroup\$ – Reinderien Oct 8 at 22:55
  • \$\begingroup\$ @Renderien, I fixed it however it might not be working properly I'll check \$\endgroup\$ – bullseye Oct 8 at 23:01
  • \$\begingroup\$ @RobbieAwesome This is not working code because it's incomplete last_thing and correct_order are not defined, fix it or this will count as off-topic and most probably will be closed. \$\endgroup\$ – bullseye Oct 8 at 23:04
  • 3
    \$\begingroup\$ Then edit your post and provide a full working code \$\endgroup\$ – bullseye Oct 8 at 23:09
  • 1
    \$\begingroup\$ @Reinderien It looks like maybe I jumped to early conclusions, it's currently working, I tested it. \$\endgroup\$ – bullseye Oct 9 at 1:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.