What are the uses of sorting algorithms like quick sort? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What are the uses of sorting algorithms like quick sort?

sorting algorithms

20th Oct 2017, 5:42 PM
Ironrobot
Ironrobot - avatar
5 Answers
+ 1
Quick sort: When you don't need a stable sort and average case performance matters more than worst case performance. A quick sort is O(N log N) on average, O(N^2) in the worst case. A good implementation uses O(log N) auxiliary storage in the form of stack space for recursion. Merge sort: When you need a stable, O(N log N) sort, this is about your only option. The only downsides to it are that it usesO(N) auxiliary space and has a slightly larger constant than a quick sort. Thereare some in-place merge sorts, but AFAIK they are all either not stable or worse than O(N log N). Even the O(N log N) in place sorts have so much larger a constant than the plain old merge sort that they're more theoretical curiosities than useful algorithms. Heap sort: When you don't need a stable sort and you care more about worst case performance than average case performance. It's guaranteed to be O(N log N), and uses O(1) auxiliary space, meaning that you won't unexpectedly run out of heap or stack space on very large inputs. Introsort: This is a quick sort that switches to a heap sort after a certain recursion depth to get around quick sort's O(N^2) worst case. It's almost always better than a plain old quick sort, since you get the average case of a quick sort, with guaranteed O(N log N) performance. Probably the only reason to use a heap sort instead of this is in severely memory constrained systems where O(log N) stack space is practically significant.
20th Oct 2017, 6:32 PM
sayan chandra
sayan chandra - avatar
+ 1
Insertion sort: When N is guaranteed to be small, including as the base case of a quick sort or merge sort. While this is O(N^2), it has a very small constant and is a stable sort. Bubble sort, selection sort: When you're doing something quick and dirty and for some reason you can't just use the standard library's sorting algorithm. The only advantage these have over insertion sort is being slightly easier to implement. Non-comparison sorts: Under some fairly limited conditions it's possible to break the O(N log N) barrier and sort in O(N). Here are some cases where that's worth a try: Counting sort: When you are sorting integers with a limited range. Radix sort: When log(N) is significantly larger than K, where K is the number of radix digits. Bucket sort: When you can guarantee that your input is approximately uniformly distributed.
20th Oct 2017, 6:33 PM
sayan chandra
sayan chandra - avatar
0
what is a stable sort?
20th Oct 2017, 6:36 PM
Ironrobot
Ironrobot - avatar
20th Oct 2017, 6:37 PM
sayan chandra
sayan chandra - avatar
- 1
all sorting algorithm names... description... http://www.geeksforgeeks.org/sorting-algorithms/
20th Oct 2017, 6:41 PM
sayan chandra
sayan chandra - avatar