+ 1

Sort list

https://code.sololearn.com/cBz9g1GfxWx4/?ref=app Can someone help me with this code? I need a list that is sorted correctly. But I would work linear In this case from left to right

26th Nov 2019, 12:24 PM
Sahra.P
Sahra.P - avatar
3 Réponses
+ 3
Sahra, there are some issues in the code, which has to be fixed. You use the name *sort* for your function, as well as for a variable name that holds True /False. *sort* is alo a method for some sequenses, this can lead to trouble. An other issue is, that var i is not defined but used in the code. And: You compare 2 adjascent values, but there is no change of position or swap for these values. When i have fixed everysting except the the last point, code runs without error, but it does no sorting. Also the last line function and in total code should be reviewed. here is a reworked code: def sort_(liste): sortx = False while sortx != True: sortx = True for i in range (0,len(liste) -1): if (liste[i] > liste [i+1]): sortx = False liste[i], liste[i +1] = liste[i + 1], liste[i] return liste liste = [9,5,7,8,3,0,2,1,4,6] print (sort_(liste))
26th Nov 2019, 2:03 PM
Lothar
Lothar - avatar
+ 1
You can use the build-in function `sort` or `sorted`. some_list.sort() print(some_list) or print(sorted(some_list))
26th Nov 2019, 1:00 PM
o.gak
o.gak - avatar
0
yes i know this is very easy but i need to work linear from legt to right with index and -1
26th Nov 2019, 1:02 PM
Sahra.P
Sahra.P - avatar