Sorting in python randomly


#1

I need to sort a list in python by following the steps.
Step 1: Import the randint definition of the random library of python.

Step 2: Take the elements of the list_1 as input.

Step 3: randomly choose two indexes i and j within the range of the size of list_1.

Step 4: Swap the elements present at the indexes i and j. After doing this, check whether the list_1 is sorted or not.

Step 5: Repeat step 3 and 4 until the array is not sorted.

In this i had completed upto step4 but i don not have a idea on how to check whethe a list is sorted my code here is.

import random

list_1=[int(x) for x in input().split()]
l=len(list_1)
while l!=0:
i=random.randint(1,l)
j=random.randint(1,l)
list_1[i], list_1[j] = list_1[j], list_1[i]


#2
from random import randint

When does l ever change?


#3

Sure you do.
If you look at it yourself, would you be able to tell? Great. Make your program do that.


#4

I made some changes to my program to sort but for some cases it shows error Index out of range

pls help me to solve it

the modified code is:

import random
l=[]
n= int(input())
for i in range(n):
l.append(int(input()))
print(l)
while(n!=0):
i=random.randint(0,n)
j=random.randint(0,n)
for i in range(n):
temp=l[i]
l[i]=l[j]
l[j]=temp
if(sorted(l)==l):
print(l)
n=n-1


#5

It is much easier to tell if something is sorted, than to sort it (and requiring another sorting algorithm to sort everything as part of your algorithm doesn’t seem right)

What does sorted mean, what is the definition of that?
This is something that is very easy to test.


#6

For future reference, when one is asking others for help debugging some problem code it’s a lot easier for us to do that if you give us the actual error.

Python doesn’t throw the Traceback because it’s pretty; it’s to help you debug.

It also helps if you post your code correctly. Use the “Preformatted text” option to maintain formatting. (This is CTRL + SHIFT + C on your keyboard.) Replace the Preformatted text with your code.

In this case, I’m fairly sure your problem - that is, the IndexError you’re getting - is coming from here:

randint(a, b) returns a random value, N, such that a <= N <= b.

In your code you’re asking the user to define the length of the random list, l, with this line: n = int(input()). n is therefore the length of your list.

However, list indexing starts at 0 so the last item in your list is at index n-1 and so you end up with a chance that randint(0,n) will return a value of n - which is out of bounds.

Hope that helps explain why you’re getting the error. Fixing it won’t get you the right result, mind; but it’ll fix the index error. :slight_smile:


#7
a = input.split()
n = len(a)

# ...

    i = randint(1, n) - 1
    j = i
    while j == i:
        j = randint(1, n) - 1

The above ensures that i and j are always different so that a swap actually takes place on each pass.

I don’t know why the author would insist upon using randint, when randrange is more appropriate for working with lists.

i = randrange(0, n)

will never produce an index that out of range.


Consider,

>>> def get_pair(s):
	s = s if isinstance(s, list) else range(s)
	n = len(s) or 1
	i = randint(0, n)
	j = i
	while j == i:
		j = randint(0, n)
	return i, j

>>> get_pair(10)
(6, 2)
>>> get_pair(10)
(9, 4)
>>> get_pair(10)
(10, 9)
>>> 

On the last one, i is out of range.

>>> get_pair(0)
(0, 1)
>>> get_pair(0)
(0, 1)
>>> get_pair(0)
(1, 0)
>>> 
>>> def get_pair(s=0):
	s = s if isinstance(s, list) else range(s) if isinstance(s, int) else []
	n = len(s) or 1
	i = randint(0, n)
	j = i
	while j == i:
		j = randint(0, n)
	return i, j

>>> get_pair()
(0, 1)
>>> get_pair('10')
(1, 0)
>>> get_pair(-3)
(1, 0)
>>>