sorting algorithm? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

sorting algorithm?

https://code.sololearn.com/c6G8f98NYiKF/#py Can someone help me with my sorting algorithm code? I'm new to data structure&algo

23rd Mar 2019, 2:10 AM
Khai123
Khai123 - avatar
15 Answers
+ 6
I am not angry in any way :) So let me explain you how the actual code works. Firstly, we know that this shopping process takes at most len(list) iterations. Then what we are doing is scanning through the list. If the number is not in order, we swop the two elements (mind you, the elements not their values, because swopping integers is useless, only swopping elements in a list).
23rd Mar 2019, 2:29 AM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
23rd Mar 2019, 2:20 AM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 4
So I have pointed out your errors. Look at //Lana Wilson's code and correct your code.
23rd Mar 2019, 2:32 AM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 4
This is going to be my 3rd year since I started Python. Well my advice is, search online, practice and code it out.
23rd Mar 2019, 2:35 AM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 4
There're still some errors Give you an advice: Print the result after every change or even every time in the loop, then you'll know how your code perform, so that you can find the problems more easily😀
23rd Mar 2019, 4:02 AM
Flandre Scarlet
Flandre Scarlet - avatar
+ 4
Go through your code and check what each line does. Try to think with numbers, not with variable names. For example, what happens if first = 8 and second = 5? first is > than second, so you want to swap them. But what does your code do? first = 8 second = 5 first = second # first = 5 second = first # second = 5 Both variables are 5 now, that's not what you want. You need to use either a temp variable: temp = first first = second second = temp or swap them the python way: first, second = second, first HOWEVER, first and second are just "labels" that point to values of the list. Swapping them won't affect the actual list elements. So instead of using first and second, you need to apply the swapping logic to the actual list elements: if l[i] > l[i+1]: l[i], l[i+1] = l[i+1], l[i]
23rd Mar 2019, 6:09 AM
Anna
Anna - avatar
+ 3
Ah bubble sort I guess
23rd Mar 2019, 2:18 AM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 3
Khai123 I hate to break this to you, but your code has a lot of errors. 1) You used range(len(x)), then you did x[i+1], that means at some point you are gonna get an array index that is too big. 2) first=second, second=first What these lines do is to just copy the value of second over to first. 3) You just copied a variable. As an integer, they do not affect the list in any way. 4) The const variable serves no purpose. These are the reasons why I provided a whole new fresh code.
23rd Mar 2019, 2:25 AM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 3
I’m sr if i make you angry. Although I cannot come up with a solution to solve this prob, i dont want to copy other codes.)))
23rd Mar 2019, 2:27 AM
Khai123
Khai123 - avatar
+ 1
No i mean what can I do to make my own code run?
23rd Mar 2019, 2:21 AM
Khai123
Khai123 - avatar
+ 1
I already watched youtube videos. And they explained exactly the way you explain. However, when I tried to implement it on my own, I failed.
23rd Mar 2019, 2:31 AM
Khai123
Khai123 - avatar
+ 1
Thank you, how long have you learnt programming? I have tried a lot of ways, but i cannot get get better as i expected.)) Have some advices?
23rd Mar 2019, 2:34 AM
Khai123
Khai123 - avatar
+ 1
thanks,bro. Really aprreciate your help:)
23rd Mar 2019, 2:36 AM
Khai123
Khai123 - avatar
0
Can you show me how to fix the code? I think i got the logic of this algo. But the prob is I cannot implement it successfully
23rd Mar 2019, 2:19 AM
Khai123
Khai123 - avatar
0
thanks for your help
23rd Mar 2019, 12:26 PM
Khai123
Khai123 - avatar