list - Trying to create a sorting algorithm from scratch in Python -


i'm taking course on programming (i'm complete beginner) , current assignment create python script sorts list of numbers in ascending order without using built-in functions "sorted".

the script started come laughably convoluted , inefficient, i'd try make work eventually. in other words, don't want copy else's script that's better.

also, far can tell, script (if ever functioned) put things in new order wouldn't ascending. start, though, , i'll fix later.

anyway, i've run in several problems current incarnation, , latest runs forever without printing anything.

so here (with hashes explaining trying accomplish). if on , tell me why code not match explanations of each block supposed do, great!

# numbers inputted, numlist = [1, 25, 5, 6, 17, 4]                       # final (hopefully sorted) list numsort = []  # index = 0  # run loop until run out of numbers while len(numlist) != 0:                                             # if there's 1 value left in numlist,     # attach end of numsort.     # (variable 'basket' transporting numbers)     if len(numlist) == 1:                                                basket = numlist.pop()         numsort.append(basket)      # rest of elifs supposed compare values     # sit next each other.      # if there's still number in numlist after 'i'     # , 'i' smaller next number     # pop 'i' , attach end of numsort     elif numlist[i+1] != numlist[-1] , numlist[i] < numlist[i+1]:            basket = numlist.pop(i)                                          numsort.append(basket)      # if there's not number after 'i'     # compare 'i' first number in list instead.     elif numlist[i+1] == numlist[-1] , numlist[i] < numlist[0]:               basket = numlist.pop(i)                                          numsort.append(basket)      # if 'i' last number in list     # , has nothing compare to,     # start on , go through again     # beginning     elif numlist [i+1] == numlist[-1]:         = 0      # if 'i' not @ end of numlist yet     # , 'i' not smaller next number     # , there still numbers left     # move on next pair     # , continue comparing , moving numbers     else:                                                                = i+1  # these in ascending order eventually. print(numsort) 

your conditions essentially:

  1. if there 1 number in list
  2. the current number less next, not equal last number
  3. the current number less first number, , next number equal last number
  4. the next number equal last number

if trace out code hand see how in many cases, none of these evaluate true , "else" executed , incremented. under these conditions, numbers never removed original list (none of elifs catch them), increment until next number equal last number, reset zero, repeat. stuck in infinite loop. need update if-statement in such way numbers caught block other final elif.

on separate note, potentially comparing number 1 number in current list before appending "sorted" list. need compare number want add "sorted" list , find proper place in list rather merely appending.

you should consider finding end of list using method more like

if == len(numlist) - 1 

this compare index length of list rather comparing more values in list not relevant order trying create.


Comments

Popular posts from this blog

html - Styling progress bar with inline style -

java - Oracle Sql developer error: could not install some modules -

How to use autoclose brackets in Jupyter notebook? -