Phase Summary (four) - Why is the same as O (n log n), quick sort of performance is better than the heap sort?

In the average case, time complexity quick sort and heap sort is O (n log n), the time complexity HEAPSORT even more stable than the complexity of quicksort time.

Because quicksort worst case time complexity is O (n²).

However, in the actual software development, quick sort of performance than good heap sort. why?

 

There are two reasons

 

Heap sort data access is no quick way to sort Friendly

 

For quick sort, the data is sequentially accessed.

For heap sort, the data is accessed dancing.

For example the process stack in the heap sort, heap on the top of the stack will be sequentially accessed array subscript 1,2,4,8 element,

Rather than quick sort as partial sequential access, so, so that the CPU cache unfriendly.

 

For the same data, in the sorting process, the number of times the data exchange heap sort algorithm to sort more quickly

Sort concept of the degree of order and reverse degrees

For sorting algorithm is based on the comparison, the entire sorting process is composed of two basic operations, compare and swap (or move).

The number of quick sort data exchange no more than reverse degrees.

The first step is to build the heap sort heap, pile construction process will disrupt the relative order of the original data, resulting in lower degree of order of the original data.

For example, for a set of data has been ordered, the construction of the heap after passing the data but became more disorderly. 

Published 113 original articles · won praise 25 · views 30000 +

Guess you like

Origin blog.csdn.net/qq_42006733/article/details/104697369