What's algorithm of ordination the library Thrust (CUDA) uses? (i.e. quick sort, merge sort)
AFAIK Thrust uses two sorting algorithms:
Merge Sort
Radix Sort
Which algorithm is used depends on the data type being sorted (Radix Sort is used for primitive types, Merge Sort for the rest).
Related
I just wrote an essay about the efficiency and usefulness of different sorting algorithms. I concluded that merge sort and quicksort were far better when sorting completely randomized lists. I just wanted to ask in what situations would the slower sorting algorithms for this scenario (bubble sort and selection sort) be more useful or as useful as quicksort and merge sort.
Notice that both merge sort and quicksort may require additional memory, whether this is heap space required to save the stack for recursion or actual copy of the buffer.
Bubble sort and selection sort does not require additional memory. So in situations where memory is a strict restriction they would be used.
Recursion involved in quick sort or merge can be expensive when we are soting quite a few numers (may be less than 100)
In this case insertion sort performs much better.
Even the standard implementations of sorting libraries in Java uses a mix of quick-merge and insertion sort
HTH
There are stable sorting algorithms which keep items that compare equal arranged in the original order. If this is important to you, then you would use a stable sorting algorithm even if slower.
There are some cases where simpler algorithms are faster (because obviously a slower algorithm is never more useful if it doesn't have other advantages).
I have seen one situation where an array was sorted, and then each array element was modified by a tiny amount. So most items were in the right position, and only a few needed exchanging. In that case shakersort proved optimal.
If you know that the array was sorted, and then a small number of items was changed, there is a clever algorithm for that (which you find somewhere on cs.stackexchange.com): If k items have been changed, you can extract at most 2k items into a separate array, sort that (using Quicksort most likely) and merge the two arrays.
If you use a library function, it is unlikely to be plain Quicksort. For example, Apple's implementation looks for a sorted range at the beginning and the end of the array, and if there are substantial numbers of items already sorted, takes advantage of this (for example sorting the concatenation of two sorted arrays runs in linear time).
I've come to understand that when sorting say a LinkedList, it's better to use merge sort because when you want to put something in the middle of the list you do it in O(1) (constant) time and O(1) memory. therefore merge sort can be done without using up extra space.
My questions are: which sorting algorithm should one use on an ArrayList? Does it matter which datatype you're sorting?
Please correct me if I'm wrong about LinkedList or let me know if I've missed something (some argument for using merge for example).
Short of copying a list to an array, sorting the array, and then creating a new list, the fastest list sort is a bottom up merge sort:
https://en.wikipedia.org/wiki/Merge_sort#Bottom-up_implementation_using_lists
Assuming ArrayList is implemented as an array of pointers to objects, and that sorting an ArrayList sorts the pointers as opposed to sorting the objects, then merge sort is faster than quick sort, since merge sort does more moves but fewer compares than quick sort, and in this case, only the pointers are being moved, while the compares involve indirection via the pointers.
As I found, you are seeking for in-place sorting algorithms (to know more see here):
In computational complexity theory, the strict definition of in-place algorithms includes all algorithms with O(1) space complexity, the class DSPACE(1).
Moreover, you can see list of in-place sorting algorithms here.
Heap sort and smooth sort are kinds of these algorithms.
JDK 7 uses Tim sort for object array. Which algorithm used for primitive array sort ?
It is a Dual-Pivot Quicksort based on this paper.
Java uses Dual-pivot QuickSort to sort array containing primitive data.
And it uses Insertion sort if size of array is small (less than 17) and if the size of array is greater than 17 it uses TimSort (Also know as "variation of merge sort") to sort the array containing the object.
Tim sort is also used in sorting of collections in java.
Do these sorting algorithms have any use in real world application?
Or is it just a basic example of sorting algorithm with n^2 complexity?
Can anyone give some example of its usage?
Insertion sort is one of the fastest sorting algorithm for sorting very small arrays.
In practice, many quicksort / mergesort implementations stop when the subarrays to sort is below certain threshold, and insertion sort is then used for these small arrays.
Selection sort is rarely used in practice.
Insertion sort is actually pretty fast for small input sizes, due to the small hidden constants in its complexity. Upto some size, insertion sort is faster than merge sort.
Thus, for many popular sorting algorithms, when the array size becomes very small, insertion sort is employed.
Bottomline: A O(N2) algorithm may be faster in practise than a O(N*logN) algorithm for sufficiently small sized inputs, owing to the hidden constants.
Yes, insertion sort is widely used in industrial applications. That's mainly dues ot the fact that several popular C++ standard libraries such as libstdc++ and libc++ implement sort routine as a combination of insertion sort and depth-limited quicksort.
The idea is that insertion sort works very fast on nearly-sorted arrays, while for a straightforward implementation of quick sort sorted input leads to the worst-case behavior. Therefore the combined algorithm first applies a quicksort-like algorithm to partially sort the input, and then finished off with a call to insertion sort.
In libc++ insertion sort is also used for sorting by default if the input size is small enough (but larger than five elements, as sizes <= 5 are handled as special cases).
HP / Microsoft std::sort() is introsort, quicksort with a depth parameter that switches to heapsort if the depth becomes too deep.
HP / Microsoft std::stable_sort() is a type of timsort. It uses insertion sort to create groups of 32 sorted elements, then uses bottom up merge sort to merge the groups.
On a side note, top down merge sort is not used in any common library that I'm aware of. Instead the common library versions for both internal (memory) and external (disk) merge sorts are all variations of bottom up merge sort (like timsort). Yet in a classroom environment or on web site articles, you see more examples of top down merge sort than bottom up merge sort.
I am now working on an imprived version of merge sort. I implemented it with C++ and C#. Then compared them with the stl sort and array.sort() algorithm respectively. In C++ I have got an equal (sometimes better) result. But in C#, I had to use unsafe code for using pointers. Here the performence is not that much comparable with default sort. So, I want to know-
1. Which algorithms are used in stl and .net base class library?(Better with links)
2. Do unsafe codes has performence issues?
3. Any suggessions for me regarding measuring the performence of the new algorithm?
.NET uses a variation of Quicksort (Sedgewick's median of 3 Quicksort).
Unless you are an expert in sorting, I would be surprised if you can beat the built-in Sort over a wide range of data (including random, already ordered and reverse ordered sets). Resorting to unsafe code is usually a bad idea...
The STL sort may depend on the implementation, but (as wikipedia says) it is usually introsort, a combination of quicksort and heapsort. It must have an average complexity of O(n log n) comparisons.
.NET uses QuickSort. You can use Reflector to view the implementation in System.Collections.Generic.ArraySortHelper
In most cases, QuickSort will run faster than MergeSort, even though the worst-case execution time is longer. There have been some improvements on standard QuickSort as well I think, but I'm not certain if any of these are used.
I seem to recall STL using QuickSort as well, but I'm not completely certain.