 sorting algorithm? | Sololearn: Learn to code for FREE!

+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

+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).

+4

Here you go: https://www.sololearn.com/learn/701/?ref=app

+4

So I have pointed out your errors. Look at //Lana Wilson's code and correct your code.

+4

This is going to be my 3rd year since I started Python. Well my advice is, search online, practice and code it out.

+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😀

+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]

+3

+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.

+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.)))

+1

No i mean what can I do to make my own code run?

+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.

+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?

+1

thanks,bro. Really aprreciate your help:)

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

0