![]() |
| ||
| Sorting in Python Hello people of Python community, I'm back with (so far) strong will to learn Python, just like you. I remember once, Narue told me that good way to learn different prog. languages is to concetrate on algorithms and data structures and their implementation in language of choice. That way, she said, one can learn both algorithms and data structrues which are language independent but also at the same time can learn the programming language. I tried to follow her advice and by reading her tutorials, I manage to implement most of sorting algorithms in C. Also, I didn't have tough time with BST and linked lists. And yesterday I decided to implement a couple algorithms and data structures in Python. I manage to implement sorting algorithms well. here is what I have done. I think someone will find it usefull. I know that this can be don in more elegant way using some Python trait's but I tried to avoid thing that are Python's specific because of the following reasons: 1. I don't know nearly enough Python as I'd like to 2. These implementations are very similar to those in C and can be easily rewritten in other programming language. Of course, problem is how to implement, say a linked list in Python, since it doesn't have pointers, or even static types so how this could be implemented is pretty unclear to me: struct nodeI know, because of very powerfull data structures that exists like built in types this is not neccessary but still... Anyway I want to share my sort implementations: import random #modul random used for generating random numbers I didn't commented or explain code in detail since really valueable and detail explanations can be found on www.eternallyconfuzzled.com and I, with my poor English and programming skills, don't have minimum chance to write somthing good like that. If you like this code, or find it useful in any way, I'd be happy, if not, sorry because wasting your time and wasting space on this forum... Regards, Micko |
| ||
| Re: Sorting in Python Looks like you met Narue on the C/C++ forum, she is the best! Your Python code is great, works well. It would be a fabulous addition to the Python Snippets. It depends how you look at it, Python variables are all pointers or references to a heap memory location. Take a look at the Python Tutorials here, I have tried to take a closer look at some variables and structures. |
| ||
| Re: Sorting in Python >problem is how to implement, say a linked list in Python, since it doesn't have >pointers, or even static types so how this could be implemented is pretty unclear to me Python is kind of screwy if you're used to a strongly typed language like C++ (or even C!). To make a node, create a class: class node: |
| ||
| Re: Sorting in Python Didn't I tell you, she is the best! Well, anyway I took your sorting algorithms and timed them. Increased elements to 1000 to get better times. # timing Micko's 6 sorting algorithms 11/27/2005I understand that the Python interpreter gives repeated function calls a time penalty. That makes your recursives look worse than they should. By the way, the Python internal list sort of this particular list takes 0.082ms. For your information the Python list container does behave like an array, but it may slow external sorting down. You can get true array modules for scientific stuff, it may be interesting to test these out. Google for NumArray. |
| ||
| Re: Sorting in Python Quote:
Cheers - Micko |
| ||
| Re: Sorting in Python Your "typical results" for timing are typical results for timing a sorted list. Only bubble sort sorts a random list. Try putting listb = list( lista ) before each sort call and then sort listb. This creates a fresh copy of the original unsorted lista. Here are typical results: timing the 6 sorting algorithms with a list of 1000 integers: bubble_sort took 269.410ms. heap_sort took 10.810ms. insertion_sort took 139.228ms. merge_sort took 14.764ms. quick_sort took 7.494ms. selection_sort took 118.048ms. Note that insertion and selection sorts are now way slower than heap, merge, and quick --as they should be-- and heap and merge are about the same. |
| ||
| Re: Sorting in Python Quote:
http://www.daniweb.com/code/snippet452.html |
| ||
| Re: Sorting in Python I am starting to learn Python and have a good crasp of C++ from school. I must say, that I find this thread very interesting information. Thanks all of you who contributed! |
| All times are GMT -4. The time now is 3:12 am. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC