4 Replies - 1865 Views - Last Post: 28 August 2015 - 05:08 AM Rate Topic: -----

#1 rnty  Icon User is offline

  • D.I.C Regular

Reputation: 19
  • View blog
  • Posts: 264
  • Joined: 14-August 12

New to python and list index out of range!

Posted 27 August 2015 - 06:51 AM

def partition(alist,st,ed):
	x=alist[ed-1]
	i=st-1
	for j in range(ed):
		if alist[j]<=x:
			i=i+1
			alist[j],alist[i]=alist[i],alist[j]
	alist[ed],alist[i+1]=alist[i+1],alist[ed]
	return i+1

def quickSort(alist,start,end):
	if start<end:
		p=partition(alist,start,end)
		quickSort(alist,start,p-1)
		quickSort(alist,p+1,end)

alist = [54,26,93,17,77,31,44,55,20]
quickSort(alist,0,len(alist))
print(alist)



Traceback (most recent call last):
File "quick.py", line 18, in <module>
quickSort(alist,0,len(alist))
File "quick.py", line 13, in quickSort
p=partition(alist,start,end)
File "quick.py", line 2, in partition
x=alist[ed]
IndexError: list index out of range

Please help me with this. I am trying to implement quicksort iteratively using python. I am not new to programming. Just learning a new language.

Is This A Good Question/Topic? 0
  • +

Replies To: New to python and list index out of range!

#2 andrewsw  Icon User is online

  • lashings of ginger beer
  • member icon

Reputation: 6340
  • View blog
  • Posts: 25,569
  • Joined: 12-December 12

Re: New to python and list index out of range!

Posted 27 August 2015 - 06:53 AM

Question moved out of Discussion Lounge. You are not starting a discussion, you are asking a question about a specific problem you are having.
Was This Post Helpful? 1
  • +
  • -

#3 jon.kiparsky  Icon User is offline

  • Screw Trump (before he screws you)
  • member icon


Reputation: 10625
  • View blog
  • Posts: 18,185
  • Joined: 19-March 11

Re: New to python and list index out of range!

Posted 27 August 2015 - 07:51 AM

The first step in figuring out a problem like this is to figure out exactly what's breaking. One easy way to do this is to insert print statements to display the values of the interesting variables at various points in the program so you can see them evolve. For example, in this case I'd be interested in knowing the local values of alist and ed

You can also use a debugger, like pdb. This is what I use on the job. Look it up in the python documentation if you're interested, it lets you step through the code and examine the state of the machine in detail. Very cool once you get the hang of it.
Was This Post Helpful? 2
  • +
  • -

#4 DK3250  Icon User is offline

  • Pythonian
  • member icon

Reputation: 310
  • View blog
  • Posts: 992
  • Joined: 27-December 13

Re: New to python and list index out of range!

Posted 27 August 2015 - 03:44 PM

For sure you will get a problem in line 8 with alist[ed], as ed = len(alist).
len(alist) = 9, but valid list index is only from 0 to 8
As far as I can see, a similar problem may occour in line 7 with alist[i].

This post has been edited by DK3250: 27 August 2015 - 11:36 PM

Was This Post Helpful? 1
  • +
  • -

#5 rnty  Icon User is offline

  • D.I.C Regular

Reputation: 19
  • View blog
  • Posts: 264
  • Joined: 14-August 12

Re: New to python and list index out of range!

Posted 28 August 2015 - 05:08 AM

Thank you everyone. I got it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1