+ 4

Sorting question

def sort(A): count=0 for i in range(len(A)): for j in range(i): if A[j]>A[j+1]: temp=A[j] A[j]=A[j+1] A[j+1]=A[j] A[j+1]=temp count+=1 break if count==0: print("yes") else: ("no") A=[3,2,1,4] sort(A) print(A) if count==0: print("yes") else: ("no") Question is i want to sort the given number but first i want to check elements are sorted or not if not then swap but the elements but swap only 1 time if elements taking 2 or more swaps then simply print no. I share my code but my code is giving me error on count is not defined i don't why.

24th Apr 2022, 7:25 AM
Akash Gupta
Akash Gupta - avatar
14 odpowiedzi
+ 4
count is a local variable of your function.
24th Apr 2022, 7:38 AM
JaScript
JaScript - avatar
+ 3
Akash Gupta , my goal was only to show you how to eliminate the error with the variable count. The other part of your question I do not understand yet at all.
24th Apr 2022, 10:34 AM
JaScript
JaScript - avatar
+ 3
Akash Gupta There is an error in your bubble sort implementation. It fails on completely reversed list. Also, you can do in place swapping, no need for a temp variable. Also, why not just print the count and the check from inside the function? No need for globals and additional print statements. Korkunç TheTerrible , the reason for the nested for loop is that the process have to go over the list multiple times. Here is a RealPython link: https://realpython.com/sorting-algorithms-JUMP_LINK__&&__python__&&__JUMP_LINK/ https://code.sololearn.com/cu5XFteHSHUS/?ref=app
26th Apr 2022, 6:07 AM
Bob_Li
Bob_Li - avatar
+ 2
Akash Gupta This is exactly what JaScript is trying to say?
24th Apr 2022, 8:04 AM
Lisa
Lisa - avatar
+ 2
Lisa if i do something wrong you can tell me?
24th Apr 2022, 8:05 AM
Akash Gupta
Akash Gupta - avatar
+ 2
JaScript def sort(A): global count count = 0 for i in range(len(A)): for j in range(i): if A[j]>A[j+1]: temp=A[j] A[j]=A[j+1] A[j+1]=A[j] A[j+1]=temp count+=1 break if count==1: print("yes") else: ("no") A=[3,1,2,4] print(A) sort(A) print(count) print("no") Actually I don't remember to remove if statement after i call the function now that's the actual code you want to swap only 1 time if elements takes 2 or more than print no if it taking 1 swap than print yes
24th Apr 2022, 8:11 AM
Akash Gupta
Akash Gupta - avatar
+ 2
Ah yes now it works thanks to Lisa .
24th Apr 2022, 8:22 AM
JaScript
JaScript - avatar
+ 1
JaScript yes but when i declared outside the function it giving me error of unboundlocalerror : local variable count referenced before assessment.
24th Apr 2022, 7:50 AM
Akash Gupta
Akash Gupta - avatar
+ 1
The print() in the else-part in the function is missing?
24th Apr 2022, 8:19 AM
Lisa
Lisa - avatar
+ 1
Lisa Hi, Lisa. Can you help me understand why the second loop with j needs to be introduced in this code? Looking at it, I thought list had items that were lists themselves (is it called an array?). Then looking down I saw that A was a list. Why is this not coded as [A]i but instead as [A]j?
24th Apr 2022, 8:45 AM
Korkunç el Gato
Korkunç el Gato - avatar
+ 1
JaScript it's a codeshef problem bro you can search if you want to pseudo sorting codeshef problem in Google
24th Apr 2022, 10:08 AM
Akash Gupta
Akash Gupta - avatar
+ 1
JaScript okay np
24th Apr 2022, 4:12 PM
Akash Gupta
Akash Gupta - avatar
+ 1
Hello I'm new here. And I really want to learn a lot of code who can help me (thank you so much))
25th Apr 2022, 8:26 PM
Habib Ibronov
Habib Ibronov - avatar
0
Hy
26th Apr 2022, 6:01 AM
JASWAL Sujal