插入排序算法,使用小数组
Insertion Sort Algorithm, using a small array
只是想知道以下是否正确。我正在尝试使用插入排序算法来解决这个问题。我尝试尝试以下数组并得出这些答案。请你告诉我这是否正确,以及我是否有正确的理解。非常感谢。
反向排序序列:
15, 12, 10, 4
12, 15, 10, 4
12, 10, 15, 4
12, 10, 4, 15
10, 12, 4, 15
10, 4, 12, 15
10, 4, 12, 15 (no swap)
4, 10, 12, 15
带有注释的序列:
15, 12, 10, 4 start
12, 15, 10, 4 1st outer loop [0:1]
12, 10, 15, 4 2nd outer loop [1:2]
10, 12, 15, 4 [0:1]
10, 12, 4, 15 3rd outer loop [2:3]
10, 4, 12, 15 [1:2]
4, 10, 12, 15 [0:1]
As stated in LECTURE NOTES ON DATA STRUCTURE on page
176
Suppose, you want to sort elements in ascending as in above figure.
Then,
Step 1: The second element of an array is compared with the elements
that appears before it (only first element in this case). If the
second element is smaller than first element, second element is
inserted in the position of first element. After first step, first two
elements of an array will be sorted.
Step 2: The third element of an array is compared with the elements
that appears before it (first and second element). If third element is
smaller than first element, it is inserted in the position of first
element. If third element is larger than first element but, smaller
than second element, it is inserted in the position of second element.
If third element is larger than both the elements, it is kept in the
position as it is. After second step, first three elements of an array
will be sorted.
Step 3: Similary, the fourth element of an array is compared with the
elements that appears before it (first, second and third element) and
the same procedure is applied and that element is inserted in the
proper position. After third step, first four elements of an array
will be sorted.
只是想知道以下是否正确。我正在尝试使用插入排序算法来解决这个问题。我尝试尝试以下数组并得出这些答案。请你告诉我这是否正确,以及我是否有正确的理解。非常感谢。
反向排序序列:
15, 12, 10, 4
12, 15, 10, 4
12, 10, 15, 4
12, 10, 4, 15
10, 12, 4, 15
10, 4, 12, 15
10, 4, 12, 15 (no swap)
4, 10, 12, 15
带有注释的序列:
15, 12, 10, 4 start
12, 15, 10, 4 1st outer loop [0:1]
12, 10, 15, 4 2nd outer loop [1:2]
10, 12, 15, 4 [0:1]
10, 12, 4, 15 3rd outer loop [2:3]
10, 4, 12, 15 [1:2]
4, 10, 12, 15 [0:1]
As stated in LECTURE NOTES ON DATA STRUCTURE on page 176
Suppose, you want to sort elements in ascending as in above figure. Then,
Step 1: The second element of an array is compared with the elements that appears before it (only first element in this case). If the second element is smaller than first element, second element is inserted in the position of first element. After first step, first two elements of an array will be sorted.
Step 2: The third element of an array is compared with the elements that appears before it (first and second element). If third element is smaller than first element, it is inserted in the position of first element. If third element is larger than first element but, smaller than second element, it is inserted in the position of second element. If third element is larger than both the elements, it is kept in the position as it is. After second step, first three elements of an array will be sorted.
Step 3: Similary, the fourth element of an array is compared with the elements that appears before it (first, second and third element) and the same procedure is applied and that element is inserted in the proper position. After third step, first four elements of an array will be sorted.