Error in End sort | SoloLearn: Learn to code for FREE!

+1

Error in End sort

https://code.sololearn.com/c6D0DZzhgXva/?ref=app Here's error in this code i am not getting desired output i have to count number of moves using end sort means we have to sort the list by moving the element at the end of the list input 1 2 3 4 5 output 0 input 1 3 5 2 6 output 3 input 5 1 3 2 7 output 3 please help

3/3/2019 4:27:32 PM

कामेश

10 Answers

New Answer

+2

I have found a similar question: https://www.sololearn.com/Discuss/1712984/?ref=app I have been solving the wrong problem. It was not a sorting problem as i thought before. 😃

+2

Let's input list is [2,5,4,3,6]. 1st iteration: [2,5,4,3,6] 2nd iteration: [2,4,3,6,5] Is that so?

+2

kamesh shekhar prasad, there's no need to modify the list to calculate movings. There's my solution: https://code.sololearn.com/coYD7WIzq3cz/?ref=app

+1

I haven't gone deep in the algorithm, but problem may lurk in these lines: for i in range(0,len(n)): # ..... for j in range(i+1,len(n)): if c==1: i=0 j=1 c=0 If you are trying to reset the loop by changing value of the loop variable it doesn't succeed because the variable is set to the next value of the range sequence every loop iteration, whether it have been changed or not.

+1

Can you explain how the code is expected to work? What should it do with the input list [2,5,3,4], for example?

+1

Ok. I've got it. Firstly there is a problem with changing the loop variable in the 'for' loop: if you change the loop variable inside 'for' it does not affect to the loop. For example: https://code.sololearn.com/c8KF9lxWM2qR/?ref=app You should use a 'while' if you want affect it.

+1

Secondly, you can avoid an extra loop and make your code simpler if you try building list methods. One way to move i-th element of the list to the end is lst.append(lst.pop(i)) There is my code of sorting algorithm as I understand it from your explaining. I hope it will help. Good luck! And thanks for opening for me this algorithm. I find it interesting https://code.sololearn.com/ciOWLKw3SFhD/?ref=app

0

@portpass i skip this code of i inside for loop but still error came

0

@portpass if the input is 2 5 3 4 then end sort will work like this iteration1 2 is less than 5 so it will do nothing 5 is greater than 3 so 5 will go at last of list 2 3 4 5 and remaining come forward like moving left so whenever we find larger element we should send that element to last of list that's the logic behind end sort

0

@portpass yes