惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

D
Docker
G
Google Developers Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
GbyAI
GbyAI
V
Vulnerabilities – Threatpost
Hugging Face - Blog
Hugging Face - Blog
I
Intezer
S
Securelist
Forbes - Security
Forbes - Security
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Jina AI
Jina AI
Y
Y Combinator Blog
N
News | PayPal Newsroom
S
Schneier on Security
O
OpenAI News
T
The Blog of Author Tim Ferriss
V
Visual Studio Blog
Simon Willison's Weblog
Simon Willison's Weblog
Martin Fowler
Martin Fowler
人人都是产品经理
人人都是产品经理
雷峰网
雷峰网
NISL@THU
NISL@THU
阮一峰的网络日志
阮一峰的网络日志
WordPress大学
WordPress大学
N
News and Events Feed by Topic
Microsoft Azure Blog
Microsoft Azure Blog
P
Proofpoint News Feed
The Cloudflare Blog
Last Week in AI
Last Week in AI
博客园 - 司徒正美
L
LangChain Blog
C
CERT Recently Published Vulnerability Notes
L
LINUX DO - 热门话题
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
aimingoo的专栏
aimingoo的专栏
Apple Machine Learning Research
Apple Machine Learning Research
Recent Commits to openclaw:main
Recent Commits to openclaw:main
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
The Hacker News
The Hacker News
博客园 - Franky
Attack and Defense Labs
Attack and Defense Labs
Security Latest
Security Latest
T
Tailwind CSS Blog
博客园_首页
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Microsoft Security Blog
Microsoft Security Blog
V2EX - 技术
V2EX - 技术
腾讯CDC
V
V2EX

博客园 - Jacky

Four early-PayPal entrepreneurial culture norms →How Facebook Ships Code Constraints on Type Parameters (C# Programming Guide) An Introduction to C# Generics http://arstechnica.com Scaling memcached at Facebook httpserver,下面是rfc相关信息: 目前互联网上公布出来的正文提取算法,大家可以综合比较下,一起来测试下哪个更好用。 词网--北京词网科技有限公司 http://demo.cikuu.com/cgi-bin/cgi-contex 猎兔网页正文提取 http://www.lie In a nutshell: C#中string.Format的格式参数问题 - Jacky - 博客园 mit 2 mit Process Monitor监测记录表明,QQ不仅会自动访问许多与聊天无关的程序和文档,例如“我的文档”等敏感位置,测试当天的上网记录也没能幸免。随后,QQ还会产生大量网络通讯,很可能是将数据上传到腾讯服务器。短短10分钟内,它访问的无关 搜索引擎高级搜索技巧 harvard 十年了,馅饼西施的馅饼还是那么正 她讲课的缺点是讲起课来波澜不惊,按着课本步步来.容易看不下去,不过确实能进一步学到一些知识。喜欢的顶起来 9。指针参数的内存传递。 - Jacky - 博客园 模块间调用重载new和delete问题 转 内存泄露检测,重载new
A Basic Comparison of Heap-Sort and Quick-Sort Algorithms
Jacky · 2010-11-18 · via 博客园 - Jacky

A Basic Comparison of Heap-Sort and Quick-Sort Algorithms

Merciadri Luca

Luca.Merciadri@student.ulg.ac.be

Abstract. We summarize here the biggest differences between Heap-sort and Quick-sort algorithms,

two useful algorithms for array sorting.

Keywords: algorithms, sort, heap-sort, quick-sort.

1 Introduction

It is well known that, to learn the complexity (denoted by O) of an algorithm, it is important to find a

resource-costly operation. Here, we describe the complexity of the Heap-sort and Quick-sort algorithms,

evidently depending upon the time T. These are useful algorithms for array sorting.

2 Heap-Sort

The heap-sort algorithm is an in situ algorithm: it does not need supplementary memory to work.

2.1 How It Works

The idea is to look at the array to sort as it was a binary tree. The first element is the root, the second is

descending from the first element, . . .A binary tree is drawn at Figure 1.

The aim is to obtain a heap, thus a binary tree verifying the following properties:

1. The maximal difference between the two leaves is 1 (all the leaves are on the last, or on the penultimate

line);

2. The leaves having the maximum depth are “heaped” on the left;

3. Each node has a bigger (resp. lower) value than its of its two children, for an ascending (resp. descending)

sort.

2.2 Complexity

The complexity of the heap-sort algorithm, for sorting a N elements array, is O(_N · log2 N). The proof is

given at Subsection 3.2, p. 2 assuming _ is known.

Fig. 1. Example of a binary tree.

3 Quick-sort

The quick-sort algorithm is not an in situ algorithm: it does need supplementary memory to work. This

stack memory can vary between log2 N and N. This algorithm is generally the most used one. We shall see

after (see Subsection 3.3, p. I) why.

3.1 How It Works

The idea is to choose a pivot element, and to compare each element to this pivot element. If an element is

inferior or equal to the pivot element, this element is put on the left of the pivot element. If the element is

strictly superior to the pivot element, it is put on the right of the pivot element. This operation is called

partitioning, and is recursively called until all the elements are sorted.

3.2 Complexity

The complexity of the quick-sort algorithm, for sorting a N elements array, is not always O(_N · log2 N)

(assuming _ is known). In fact, it can vary between _N · log2 N and _N2 (assuming _ is known).

O(N · log2

N) The complexity of the quick-sort algorithm is essentially O(N · log2 N) when the array’s

elements are not almost all sorted (assuming the pivot element is the first one of the array). The complexity

is the same (close to a _

_ factor) for the heap-sort algorithm.

Proof. Let tq, N and th denote respectively the time for the quick-sort algorithm if the elements have not

already been sorted, the number of elements of the array, and the time for the heap-sort algorithm, whatever

the case. We then have, knowing that tq = th, and considering1 each rearrangement gives two subzones of

identical sizes:

tq = _N + 2tq

_

N

2

_

= _N + 2

_

_N

2

+ 2tq

_

N

4

__

= _N + _N + 4tq

_

N

4

_

= _N + _N + 4

_

_N

4

+ 2tq

_

N

8

__

...

= _N + _N + · · · + _N + 2xtq

_

N

2x

_

| {z }

=0

= _|N + _N{+z · · · + _N}

x times with x=log2(2x)=log2(N)

= _N log2(N),

where N = 2x. ut

1 In fact, each rearrangement gives N1

2 -sized subzones, but we here want to find a magnitude, and we consider

N

2

_

N1

2 .

O

􀀀

N2_

The complexity of the quick-sort algorithm is essentially O

􀀀

N2

_

when the array’s elements are

almost all sorted (assuming the pivot element is the first one of the array).

Proof. Let tq denote the time for the quick-sort algorithm if the elements have already been sorted. We

then have

tq =

NX1

i=1

_ (N i) | {z }

at the arrangement i

= _(N 1) + _(N 2) + · · · + _

= _

_

N2 N

2

_

' _ · N2.

ut

Remark 1. For another pivot choice, there is an initial disposition of the array’s element which lands to the

unfavorable case.

3.3 Why Is It Quick, Then

One can ask why the quick-sort is the most used algorithm if its complexity has a minimum of N · log2 N,

which is the same as the quick-sort. In fact, when both algorithms have same complexity (_N · log2 N for

the quick-sort, and _N · log2 N for the heap-sort), the quick-sort is faster because he has a proportionnality

coefficient which equals the half of the heap-sort’s proportionnality coefficient; mathematically, we have

_ =

_

2

.

4 Which One to Choose

There is no “ideal” solution about arrays’ sorting. If you want to predict precisely when your algorithm will

have finished, it is better to use the heap-sort algorithm, because the complexity of this one is always the

same. It is also the case if you know in advance that your array is almost sorted.

References

1. de Marneffe, Pierre-Arnoul: Introduction `a l’algorithmique (1998) ULg.

2. Cormen, Thomas H. and Leiserson, Charles E. and Rivest, Ronald L. and Stein, Clifford: Introduction to Algo-

rithms. McGraw-Hill Book Company (2001)