Java Reference
In-Depth Information
while
( (index >= begin)
and
(anEntry < a[index]) )
{
a[index
+
1] = a[index]
//
make room
index--
}
//
Assertion:
a[index
+
1]
is available.
a[index
+
1] = anEntry
//
insert
FIGURE 8-6
Inserting the next unsorted entry into its proper location within
the sorted portion of an array during an insertion sort
Sorted
Unsorted
2
5
8
3
9
4
1
2
5
8
9
4
1
2
5
8
9
4
1
3
2
5
8
9
4
1
2
5
8
9
4
1
Sorted
Unsorted
2
3
5
8
9
4
1
FIGURE 8-7
An insertion sort of an array of integers into ascending order
8
2
6
4
9
7
1
8
2
6
4
9
7
1
2
8
6
4
9
7
1
2
6
8
4
9
7
1
2
4
6
8
9
7
1
8
2
4
6
9
7
1
7
2
4
6
8
9
1
1
2
4
6
7
8
9