?? s_ins.htm
字號:
<html>
<head>
<title>Insertion Sort</title>
</head>
<body bgcolor="#ffffff">
<p align=right>
<a href="s_man.htm" tppabs="http://www.ee.uwa.edu.au/~plsd210/ds/niemann/s_man.htm" target="_top"><img src="c_man.gif" tppabs="http://www.ee.uwa.edu.au/~plsd210/ds/niemann/c_man.gif" width=74 height=19 border=0></a>
</p>
<h1>Insertion Sort</h1>
One of the simplest methods to sort an array is an insertion sort.
An example of an insertion sort occurs in everyday
life while playing cards. To sort the cards in your hand you
extract a card, shift the remaining cards, and then insert the
extracted card in the correct place. This process is repeated
until all the cards are in the correct sequence. Both average
and worst-case time is <b><i>O</i></b>(<i>n</i><sup>2</sup>).
For further reading, consult
<a href="javascript:if(confirm('http://www.amazon.com/exec/obidos/ISBN=0201896850/none01A/ \n\nThis file was not retrieved by Teleport Pro, because it is addressed on a domain or path outside the boundaries set for its Starting Address. \n\nDo you want to open it from the server?'))window.location='http://www.amazon.com/exec/obidos/ISBN=0201896850/none01A/'" tppabs="http://www.amazon.com/exec/obidos/ISBN=0201896850/none01A/" target="_top">Knuth [1998]</a>.
<h2>Theory</h2>
Starting near the top of the array in Figure 2-1(a),
we extract the 3.
Then the above elements are
shifted down until we find the correct place to insert the 3.
This process repeats in Figure 2-1(b) with the next number.
Finally, in Figure 2-1(c),
we complete the sort by inserting 2 in the
correct place.
<p>
<center>
<h3><a name="Fig2-1">
<img src="s_fig21.gif" tppabs="http://www.ee.uwa.edu.au/~plsd210/ds/niemann/s_fig21.gif" vspace=5 width=559 height=414><br>
Figure 2-1: Insertion Sort</a>
</h3>
</center>
<p>
Assuming there are <i>n</i> elements in the array, we must index
through <i>n</i> - 1 entries. For each entry, we may need to examine
and shift up to <i>n</i> - 1 other entries, resulting in a
<b><i>O</i></b>(<i>n</i><sup>2</sup>) algorithm.
The insertion sort is an <i>in-place</i> sort. That is, we sort the
array in-place. No extra memory is required. The insertion sort
is also a <i>stable</i> sort. Stable sorts retain the original
ordering of keys when identical keys are present in the input
data.
<h2>Implementation</h2>
An
<a href="javascript:if(confirm('http://www.ee.uwa.edu.au/~plsd210/ds/niemann/s_ins.txt \n\nThis file was not retrieved by Teleport Pro, because it is linked too far away from its Starting Address. If you increase the in-domain depth setting for the Starting Address, this file will be queued for retrieval. \n\nDo you want to open it from the server?'))window.location='http://www.ee.uwa.edu.au/~plsd210/ds/niemann/s_ins.txt'" tppabs="http://www.ee.uwa.edu.au/~plsd210/ds/niemann/s_ins.txt">ANSI-C implementation</a>
for insertion sort is included.
<b>Typedef T</b> and comparison operator <b>compGT</b>
should be altered to reflect the data stored in the table.
</body>
</html>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -