7 Replies - 1825 Views - Last Post: 10 November 2009 - 03:41 PM Rate Topic: -----

#1 xenor   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 148
  • Joined: 23-September 09

Quick sort help

Posted 07 November 2009 - 01:30 PM

def qsort1(lst):
	if len(lst) <= 1:
		return lst
	pivot = lst.pop(0)
	greater_eq = qsort1([i for i in lst if i >= pivot])
	lesser = qsort1([i for i in lst if i < pivot])
	return lesser + [pivot] + greater_eq
 


Can someone please explain to me how this line is working?

qsort1([i for i in lst if i >= pivot])


Is This A Good Question/Topic? 0
  • +

Replies To: Quick sort help

#2 Dogstopper   User is offline

  • The Ninjaducky
  • member icon

Reputation: 2975
  • View blog
  • Posts: 11,224
  • Joined: 15-July 08

Re: Quick sort help

Posted 08 November 2009 - 12:13 PM

Quote

Can someone please explain to me how this line is working?

qsort1([i for i in lst if i >= pivot])



This line sets a variable i to every index(also i (the second one)) only if i >= pivot. Then with this list of true values (the ones that meet the criteria of >= pivot) the qsort() function is called again with this new list of values as it's parameters; this is called recursion.
Was This Post Helpful? 0
  • +
  • -

#3 xenor   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 148
  • Joined: 23-September 09

Re: Quick sort help

Posted 08 November 2009 - 12:33 PM

 i for

I can't understand the meaning of this?

 [] 


Lists?

This post has been edited by xenor: 08 November 2009 - 12:33 PM

Was This Post Helpful? 0
  • +
  • -

#4 Tshiknn   User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 20
  • Joined: 18-October 09

Re: Quick sort help

Posted 08 November 2009 - 05:05 PM

View Postxenor, on 8 Nov, 2009 - 11:33 AM, said:

 i for

I can't understand the meaning of this?

 [] 


Lists?


What you're looking at is a Python feature called a listcomp, or list comprehension. For example:

example1 = []
example2 = []

# these do the same thing.
example1 = [i for i in xrange(10)]
for i in xrange(10):
	example2.append(i)

Your example is a bit more complex. Here we apply a filter. Simply explained, with a filter a listcomp will check the if statement first and then if it's true add the iterator value. For example:

example1 = [i for i in xrange(10) if i % 2 == 0]

Now i is populated with [0, 2, 4, 6, 8]. Now that you understand listcomps (hopefully), your example simply calls qsort1, giving it a list populated with values more than or equal to the pivot point.
Was This Post Helpful? 1
  • +
  • -

#5 xenor   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 148
  • Joined: 23-September 09

Re: Quick sort help

Posted 09 November 2009 - 07:39 AM

Thanks a lot i understand the part that you explained to me.

 return lesser + [pivot] + greater_eq


What is the meaning of this?
That will return ......
Was This Post Helpful? 0
  • +
  • -

#6 xenor   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 148
  • Joined: 23-September 09

Re: Quick sort help

Posted 10 November 2009 - 03:08 PM

The "myth" has been solved :D

 return lesser + [pivot] + greater_eq


This part is used to connect lesser + pivot + greater_eq element that are derived from:

  
greater_eq = qsort1([i for i in lst if i >= pivot])
lesser = qsort1([i for i in lst if i < pivot])


Was This Post Helpful? 0
  • +
  • -

#7 Dogstopper   User is offline

  • The Ninjaducky
  • member icon

Reputation: 2975
  • View blog
  • Posts: 11,224
  • Joined: 15-July 08

Re: Quick sort help

Posted 10 November 2009 - 03:17 PM

There you go! Sorry for not getting back to you, i was away.
Was This Post Helpful? 1
  • +
  • -

#8 xenor   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 148
  • Joined: 23-September 09

Re: Quick sort help

Posted 10 November 2009 - 03:41 PM

Tnx to anyone who helped me. See ya.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1