![]() |
25 ways to efficiently sort an array/list of integers
Ok, this should be manageable. Only 25 solutions needed... but there's a catch. It must be efficient (i.e. better than bubblesort) and you need to explain why or how it's efficient. Algorithms can be repeated, but only if you optimize better or differently (e.g. time vs. space) than a previous entry (you need to explain what you did better or differently). Post your language just so we can tell what it is.
I'll kick it off 1) MIPS 32 - Quicksort (comments are in C-ish pseudo-code) :
.text |
Maybe this could help some people: http://en.wikipedia.org/wiki/Sorting...ing_algorithms
I will try to implement one when the storm here passes. |
:
O(n log N) - I think I read that std::sort uses introsort nowadays... EDIT: did some reading, it looks like just the SGI C++ STL uses introsort, I guess the regular C++ STL still uses quicksort... |
Introsort is a variant of quicksort. Maybe I'll do a heapsort. Maybe I'll go to the dentist, too, lol.
|
Oh, I thought it was the efficient parts of quicksort and heapsort combined.
|
I wasn't suggesting it wasn't a valid entry, just characterizing it.
|
3) Perl- Mergesort
:
sub merge { |
This is a heapsort. Rather than just pluck a function from the library and inexplicably illustrate the sort, I have implemented the sort. This approach uses an array representation for the tree. There are interesting things about the heapsort which aren't illustrated here, such as sorting in-place and the implications that has on the min/max nature of the heap. Google will be more than happy to satisfy your curiosity.
A heap may be either a min heap or a max heap. The requirements for a heap are that every parent is less (greater) than its children. The tree must also be complete; that is, every level is filled with the possible exception of the last, which is packed to the "left". The sort is accomplished by plucking out the root and placing it in the output list. It is then replaced by the most extreme element, which almost certainly breaks the heapness. The tree is then re-heaped and it's all done again. Obviously, the tree shrinks as the sort progresses. This storage may be used to accomplish the (quasi) in-place sort mentioned above. In re-heaping, one finds that only half the tree needs to be adjusted. Moving into this half, one finds that only half of the sub-hive needs to be adjusted. This is the "divide and conquer" approach that also makes sorts like quicksort effective. As for time complexity, forming a heap from a wild tree requires, worst case, 3N comparisons. The sort, which requires re-heaping (quicker than the heapify), needs, worst case, Nlog(N) comparisons. :
class Heap:Quote:
|
heapsort:
:
void heapSort(int numbers[], int array_size) |
mergesort:
:
void mergeSort(int numbers[], int temp[], int array_size) |
| All times are GMT -5. The time now is 9:17 PM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC