I want to take a list for sorting and sort it using python random library. The strategy to do this:
- First, take any two random index in the list we take as input
- Then swap those two elements.
- check if the list is sorted or not.
- if not sorted , repeat the steps again.
I am new in python. So not well acquainted with all the techniques . Please help with explanation. I cant figure out what is going wrong.
from random import randint
n=int(input())
l=[int(input()) for x in range(0,n)]
p=1
while (1):
if(p==1):
ransort(n)
else:
break
for x in l:
print (x)
def ransort(n):
i=randint(0,n-1)
j=randint(0,n-1)
l[i],l[j]=l[j],l[i]
if l== l.sort():
p=0
else:
p=1
return p
Aucun commentaire:
Enregistrer un commentaire