+ 4

Python Method Not Returning

With a break from web codes I have decided to try Python again. I have used the return keyword but nothing is returning, I even tried print, although it printed the original array not the sorted version. Can someone help fix this? Both BubbleSort and return aren't working def BubbleSort(arr): sorted = False while sorted != sorted: sorted = True for i in range(len(arr)-1): if arr[i] > arr[i + 1]: sorted = False arr[i], arr[i + 1] = arr[i + 1], arr[i] print(arr) arr1 = [83, 5, 382, 8] BubbleSort(arr1)

13th Jun 2019, 7:38 AM
Clueless Coder
Clueless Coder - avatar
7 ответов
+ 1
I implemented your code (i think) And it works https://code.sololearn.com/cpKwpXUrEfk2/?ref=app
13th Jun 2019, 8:43 AM
Loeschzwerg
+ 5
Thanks, although I would rather learn the technical side of things, I have never made a working sorting algorithm before. Thanks anyway
13th Jun 2019, 7:53 AM
Clueless Coder
Clueless Coder - avatar
+ 4
Please upload your code to the code playground and post a link here
13th Jun 2019, 8:35 AM
Anna
Anna - avatar
+ 2
Mmh your while condition is always false sorted != sorted can't ever be true, so you're stuck in an infinite loop To correct : while sorted != True (or while Not sorted)
13th Jun 2019, 8:06 AM
ThewyShift
ThewyShift - avatar
+ 2
You are not stuck in an infinite loop. While only evaluates if the condition is true, which is not
13th Jun 2019, 8:32 AM
Loeschzwerg
+ 1
Your function doesn't return anything. If so, you would use the return keyword
13th Jun 2019, 8:30 AM
Loeschzwerg
+ 1
Loeschzwerg indeed ! I made a mistake there, the condition is never true, so the whole while-block is skipped !
13th Jun 2019, 2:17 PM
ThewyShift
ThewyShift - avatar