EnglishFrenchGermanSpainItalianDutchPortugueseRussianKoreanJapaneseArabic Chinese Simplified

Monday, May 23, 2011

A Comparison of Sorting Algorithms

Recently, I have translated a variety of sorting routines into Visual Basic and compared their performance...  I hope you will find the code for these sorts useful and interesting.

 

What makes a good sorting algorithm?  Speed is probably the top consideration, but other factors of interest include versatility in handling various data types, consistency of performance, memory requirements, length and complexity of code, and the property of stability (preserving the original order of records that have equal keys).  As you may guess, no single sort is the winner in all categories simultaneously (Table 2).
 
Let's start with speed, which breaks down into "order" and "overhead".   When we talk about the order of a sort, we mean the relationship between the number of keys to be sorted and the time required.  The best case is O(N); time is linearly proportional to the number of items.  We can't do this with any sort that works by comparing keys; the best such sorts can do is O(N log N), but we can do it with a RadixSort, which doesn't use comparisons.  Many simple sorts (Bubble, Insertion, Selection) have O(N^2) behavior, and should never be used for sorting long lists.  But what about short lists?  The other part of the speed equation is overhead resulting from complex code, and the sorts that are good for long lists tend to have more of it.  For short lists of 5 to 50 keys or for long lists that are almost sorted, Insertion-Sort is extremely efficient and can be faster than finishing the same job with QuickSort or a RadixSort.  Many of the routines in my collection are "hybrids", with a version of InsertionSort finishing up after a fast algorithm has done most of the job.The third aspect of speed is consistency.  Some sorts always take the same amount of time , but many have "best case" and "worst case" performance for particular input orders of keys.  A famous example is QuickSort, generally the fastest of the O(N log N) sorts, but it always has an O(N^2) worst case.  It can be tweaked to make this worst case very unlikely to occur, but other O(N log N) sorts like HeapSort and MergeSort remain O(N log N) in their worst cases.  QuickSort will almost always beat them, but occasionally they will leave it in the dust. 

Artikel Terkait:

Comments :

0 comments to “A Comparison of Sorting Algorithms”

Post a Comment

 

Copyright © 2009 by Learn Technology

Template by Blogger Templates | Powered by Blogger