
8.6 Searching and Sorting Arrays 521
We then compare the current element, 2, stored in temp, and 17
(index j – 1 = 1). Since 2 is smaller than 17, we shift 17 to the right and
decrement j by 1; j now has the value 1.
We then compare the current element, 2, stored in temp, and 5
(index j – 1 = 0). Since 2 is smaller than 5, we shift 5 to the right and decre-
ment j by 1; j now has the value 0.
Since j is 0, we exit the inner loop and assign the value of the current ele-
ment, 2, stored in temp, to the array element at index j. The value 2 has now
been inserted.
Value
Inserted elements Uninserted element
Index
5
0
5
1
17
2
26
3
j (inner loop counter)
2
temp
Value