The insertion sort algorithm provides an alternative to the selection sort algor
ID: 3666897 • Letter: T
Question
The insertion sort algorithm provides an alternative to the selection sort algorithm for sorting small numbers of items. It's more efficient than selection sort if the array is only slightly out of order. The following code implemens the insertion sort algorithm:
10 public static void insertionSort(int[] list)
11 {
12 int temp;
13 int j;
14
15 for (int i = 1; i
16 {
17 temp = list[i];
18 for (j=i; j>0 && temp
19 {
20 list[j] = list[j-1];
21 }
22 list[j] = temp;
23 }
24}
Note that the scope of the j count variable extends beyond the scope of the for loop in which it's used. Assume that an array of int has been instantiated and the insertionSort method has been called with a reference ot this array passed in as a parameter. Trace the execution of this method, using the following header and initial entries:
This problem is also Exercise 12 in Introduction to Programming With Java 2nd Edition: Chapter 9. I am with the provided solution on this site until 2222 is put in the arr1 1 column; what I did is different at that point, and i don't know why
EDIT: I figured it out!
Sort line# insertionSort arr1 (list) i j temp length 0 1 2 3 4 3333 1234 2222 1000 10 arr1Explanation / Answer
Therefore the final list is sorted. If you still need any further clarification, just get back to me.
Sort line# insertionSort arr1 (list) i j temp length 0 1 2 3 10 arr1 4 3333 1234 2222 1000 15 arr1 1 4 3333 1234 2222 1000 17 arr1 1 1234 4 3333 1234 2222 1000 18 arr1 1 1 1234 4 3333 1234 2222 1000 20 arr1 1 1 1234 4 3333 3333 2222 1000 18 arr1 1 0 1234 4 3333 3333 2222 1000 22 arr1 1 0 1234 4 1234 3333 2222 1000 15 arr1 2 0 1234 4 1234 3333 2222 1000 17 arr1 2 0 2222 4 1234 3333 2222 1000 18 arr1 2 2 2222 4 1234 3333 2222 1000 20 arr1 2 2 2222 4 1234 3333 3333 1000 18 arr1 2 1 2222 4 1234 3333 3333 1000 22 arr1 2 1 2222 4 1234 2222 3333 1000 15 arr1 3 1 2222 4 1234 2222 3333 1000 17 arr1 3 1 1000 4 1234 2222 3333 1000 18 arr1 3 3 1000 4 1234 2222 3333 1000 20 arr1 3 3 1000 4 1234 2222 3333 3333 18 arr1 3 2 1000 4 1234 2222 3333 3333 20 arr1 3 2 1000 4 1234 2222 2222 3333 18 arr1 3 1 1000 4 1234 2222 2222 3333 20 arr1 3 1 1000 4 1234 1234 2222 3333 18 arr1 3 0 1000 4 1234 1234 2222 3333 22 arr1 3 0 1000 4 1000 1234 2222 3333Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.