The Insertion Sort algorithm provides an alternative to the selection sort algorithm for sorting small numbers of items (about 20 or less). It’s more efficient than selection sort if the array is only slightly out of order. The following code implements the Insertion Sort algorithm:
10 public static void insertionSort(int[] list)11 {12 int temp;13 int j;1415 for (int i=1; i0 && temp
Note that the scope of the
jcount variable extends beyond the scope of the
forloop in which it’s used. Assume that an array of
inthas been instantiated and the
insertionSortmethod has been called with a reference to this array passed in as a parameter. Trace the execution of this method, using the following header and initial entries:
Sort | |||||||||
| insertionSort | arr1 | |||||||
line# | (list) | i | j | temp | length | 0 | 1 | 2 | 3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 | 3333 | 1234 | 2222 | 1000 |
10 | arr1 |
|
|
|
|
|
|
|
|
We need at least 10 more requests to produce the solution.
0 / 10 have requested this problem solution
The more requests, the faster the answer.