Flash Website Builder- Trendy Site Builder is a Flash Site Building tool that helps users build stunning websites. Check Out Custom Custom Logo Design by LogoBee. Website Design and Free Logo Templates available.
 CodingForums.com Python selection sort; i am trying to do this using recursion

Before you post, read our: Rules & Posting Guidelines

Enjoy an ad free experience by logging in. Not a member yet? Register.
 12-03-2011, 01:38 AM PM User | #1 Mitko New to the CF scene   Join Date: Dec 2011 Posts: 2 Thanks: 0 Thanked 0 Times in 0 Posts Python selection sort; i am trying to do this using recursion Hey I am a first year unversity student and my project was to do a selection sort using recursion. I have figured out how to do it, but i don't know how to do it with recursion any help please? def selsort(l): """ sorts l in-place. PRE: l is a list. POST: l is a sorted list with the same elements; no return value. """ for pos in range(len(l)-1): largest = l[pos] largestpos = pos for i in range(pos+1, len(l)): if l[i] > largest: largest = l[i] largestpos = i l[pos], l[largestpos] = l[largestpos], l[pos] l.reverse()
 12-03-2011, 01:41 AM PM User | #2 Mitko New to the CF scene   Join Date: Dec 2011 Posts: 2 Thanks: 0 Thanked 0 Times in 0 Posts My main problem right now is that i have no idea what the algorithm would be. I am fairly good at translating algorithms to Python, but I am having trouble getting the basic algorithm for it Any help would be much obliged.
 12-05-2011, 07:50 PM PM User | #3 Apothem Regular Coder     Join Date: Mar 2008 Posts: 380 Thanks: 36 Thanked 25 Times in 25 Posts It's not that hard to think up of. You just need to make sure the recursion is "iterating" the array at every element (not in any particular language, but you should get it): Code: ```ssort(array, start_pos, inc_pos, swap_pos) { if(inc_pos >= array.length) { u = array[swap_pos]; array[swap_pos] = array[start_pos]; array[start_pos] = u; if(start_pos != array.length - 1) { return ssort(array, start_pos+1, start_pos+1, start_pos+1); } return array; } if(array[inc_pos] < array[swap_pos]) { return ssort(array, start_pos, inc_pos+1, inc_pos); } else { return ssort(array, start_pos, inc_pos+1, swap_pos); } } sorted_array = ssort(array, 0, 0, 0)``` Though I am not sure if this is the most efficient way. This is the fully recursive method I thought up of, but I think others tend to just use a combination of recursion and iteration (i.e. a for loop within the recursive function). Hope this helps. Last edited by Apothem; 12-05-2011 at 07:55 PM..

 Bookmarks

 Tags python, recursion, selection sort, sort, sorting

 Thread Tools Rate This Thread Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is Off Forum Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home :: Client side development     JavaScript programming         DOM and JSON scripting         Ajax and Design         JavaScript frameworks         Post a JavaScript     HTML & CSS     XML     Flash & ActionScript         Adobe Flex     Graphics and Multimedia discussions     General web building         Site reviews         Building for mobile devices :: Server side development     Apache configuration     Perl/ CGI     PHP         Post a PHP snippet     MySQL         Other Databases     Ruby & Ruby On Rails     ASP     ASP.NET     Java and JSP     Other server side languages/ issues         ColdFusion         Python :: Computing & Sciences     Computer Programming     Computer/PC discussions     Geek News and Humour Web Projects and Services Marketplace     Web Projects         Small projects (quick fixes and changes)         Medium projects (new script, new features, etc)         Large Projects (new web application, complex features etc)         Unknown sized projects (request quote)         Vacant job positions         Looking for work/ for hire         Project collaboration/ partnership         Paid work offers and requests (Now CLOSED)     Career, job, and business ideas or advice     Domains, Sites, and Designs for sale         Domains for sale         Websites for sale         Design templates and graphics for sale :: Other forums     Member Offers     Forum feedback and announcements

All times are GMT +1. The time now is 12:16 AM.