Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why we are always using quick sort ? or any specific sorting algorithm?

Tags:

algorithm

why we are always using quick sort ? or any specific sorting algorithm ??

i tried some experiment on my PC using quick,merge,heap,flash sort

results:-

sorting algorithm : time in nanosecond -> time in minutes

quick sort time : 135057597441 -> 2.25095995735

Flash sort time : 137704213630 -> 2.29507022716667

merge sort time : 138317794813 -> 2.30529658021667

heap sort time : 148662032992 -> 2.47770054986667

using java in built function

long startTime = System.nanoTime();

given times are in nanoseconds there hardly any difference between them if we convert them into seconds for 20000000 random integer data and max array size is 2147483647 in java.if we are using in-place algorithm then there may be difference of 1 to 2 min till max array size.

if the difference is too small why we should care ??

like image 937
asd Avatar asked Jan 27 '14 20:01

asd


People also ask

Why is quick sort better than other sorting algorithms?

Quick sort is an in-place sorting algorithm. In-place sorting means no additional storage space is needed to perform sorting. Merge sort requires a temporary array to merge the sorted arrays and hence it is not in-place giving Quick sort the advantage of space.

Why quick sort algorithms are mostly used in applications?

The sorting algorithm is used for information searching and as Quicksort is the fastest algorithm so it is widely used as a better way of searching. It is used everywhere where a stable sort is not needed. Quicksort is a cache-friendly algorithm as it has a good locality of reference when used for arrays.

Which sorting method is best and why?

Quicksort is one of the most efficient sorting algorithms, and this makes of it one of the most used as well. The first thing to do is to select a pivot number, this number will separate the data, on its left are the numbers smaller than it and the greater numbers on the right.

Why is quick sort better than Heapsort?

It runs fast, much faster than Heap and Merge algorithms. The secret of Quicksort is: It almost doesn't do unnecessary element swaps. Swap is time consuming. With Heapsort, even if all of your data is already ordered, you are going to swap 100% of elements to order the array.


1 Answers

All of the algorithms presented have a similar average case bounds, of O(n lg n), which is the "best" a comparison sort can do.

Since they share the same average bounds, the expected performance of these algorithms over random data should be similar - which is what the findings show. However, the devil is in the details. Here is a very quick summary; follow the links for further details.

Quicksort is generally not stable (but there are stable variations). While quicksort has an average bounds of O(n lg n), Quicksort has a worst case bounds of O(n * n) but there are ways to mitigate this. Quicksort, like heapsort, is done in-place.

Merge-sort is a stable sort. Mergesort has a worst case bounds of O(n lg n) which means it has predictable performance. Base merge-sort requires O(n) extra space so it's generally not an in-place sort (although there is an in-place variant, and the memory for a linked list implementation is constant).

Heapsort is not stable; it also has the worst case bounds of O(n lg n), but has the benefit of a constant size bounds and being in-place. It has worse cache and parallelism aspects than merge-sort.

Exactly which one is "best" depends upon the use-case, data, and exact implementation/variant.


Merge-sort (or hybrid such as Timsort) is the "default" sort implementation in many libraries/languages. A common Quicksort-based hybrid, Introsort is used in several C++ implementations. Vanilla/plain Quicksort implementations, should they be provided, are usually secondary implementations.

Merge-sort: a stable sort with consistent performance and acceptable memory bounds.

Quicksort/heapsort: trivially work in-place and [effectively] don't require additional memory.

like image 102
user2864740 Avatar answered Oct 18 '22 17:10

user2864740