开发者

comparison of sorting algorithms

When do we prefer

a) bucket sort, and b) radix sort

over comparison sorts like


Radix sort is preferable when you have to sort a lot of numbers, usually natural numbers that fit in 32 / 64 bit ints (if less, consider counting sort). This is because it's faster, doing about k*N operations, where k is a constant (O(N) time in other words). k is usually 2 or 4 for 32 bit ints.

When you have to sort smaller collections, there's no point bothering with radix sort and its kin. An optimized quick sort (read: introsort) will be faster in these cases. Also, if you're sorting custom data types, radix sort will probably even be impossible to use, so you have no choice but to use a comparison sort.

If you're not sure which is faster (and it's hard to be sure sometimes), run tests. Always consider the cases where the input is already sorted, inversely-sorted and in random order. Consider the memory requiremens of each algorithm, and make your choice accordingly.


  • Comparison of algorithms
  • Sorting Algorithms Compared
  • Slightly Skeptical View on Sorting Algorithms

Mathematicians would put it that most sorts run in O(n log(n)) or O(n²) time, where RadixSort runs in O(n) time. -source

Bucket sort is a cousin of radix sort in the most to least significant digit flavour. - source

Advantages: -copied from source

  • Radix and bucket sorts are stable, preserving existing order of equal keys.

  • They work in linear time, unlike most other sorts. In other words, they do not bog down when large numbers of items need to be sorted. Most sorts run in O(n log n) or O(n^2) time.

  • The time to sort per item is constant, as no comparisons among items are made. With other sorts, the time to sort per time increases with the number of items.

  • Radix sort is particularly efficient when you have large numbers of records to sort with short keys.


This sound a lot like a homework question, so I don't want to say too much.

Bubble sort is a very simple sort algorithm, it goes through all items of the list, and compares it to every other item. This results in a lot of comparisons, thus very slow.

Radix sort is sort based on numbers, but you can represent any data in numbers, it and bucket can give much faster results.

insertion/selection/merge sort are designed to do those tasks. for instance, when merging to lists, if demand that the two lists be pre sorted, then you can merge them quickly using a special sort, rather then just sort the entire list as one. if you know that both list are in order, you just need to keep track of where you are in each list (two index numbers) and compare the element at each index and move the index up when you take one of the items and move it into a new list.

Sorting algorithms are a huge area of computing as there are so many different requirements. The merge I described is simple to code, but while sorting, doubles the memory being used. You could probably make it run faster as well. Maybe starting at both ends, keeping track of two indexs, making two half merged lits, one going from bottom to middle, other from top to middle, then attach the second to the first... might work better I don't know.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜