<<set $needCompletion to true>>
<<goto "Introduction">>! Insertion Sort
Insertion sort is a simple and effective algorithm. It doesn't work well for large random sequences (its average run-time is quadratic), it works very well on small or mostly sorted sequences. In this activity you will simulate insertion sort on a sample list. <<if def setup.part2>>Then you will implement insertion sort on an array, using a comparator.<<else>><</if>>
[[Continue|Simulation]]Here's the current situation, with the sorted part of the array backgrounded in green.
<<if $index gte 0>>The current value being inserted is <<=$list[$index]>>.<</if>>
<table style="table-layout: fixed; width: 100%;">\
<tr style="text-align:center;"><th @colspan="$sorted">Sorted</th>\
<<if $sorted lt $list.length>><<set _unsorted to $list.length - $sorted>>\
<th @colspan="_unsorted">Unsorted</th>\
<</if>></tr>\
<tr style="text-align:center;height:40px;">\
<<for _i to 0; _i lt $list.length; _i++>><<set _v to $list[_i]>>\
<<if _i eq $index>><td @style="setup.sortedStyle">X</td>\
<<elseif _i lt $sorted>><td @style="setup.sortedStyle">_v</td>\
<<else>><td>_v</td>\
<</if>>\
<</for>>\
</tr></table>[[Back|Introduction]]
!! Simulation
<span id="simulation"><<include "SIMULATION">></span>
Choose what action should happen next:
* <<link "Start inserting the next value">>
<<if $index gte 0>>
<<replace "#response" t8n>>We're not done with the current value.<</replace>>
<<elseif $sorted eq $list.length>>
<<replace "#response" t8n>>There's no more to insert. \
You can finish the simulation.<</replace>>
<<else>>
<<set $index to $sorted>>
<<set ++$sorted>>
<<replace "#response">><</replace>>
<<replace "#simulation">><<include "SIMULATION">><</replace>>
<</if>><</link>>
* <<link "Move the hole towards the start of the array">>
<<if $index lt 0>>
<<replace "#response" t8n>>We don't have a current value.<</replace>>
<<elseif $index eq 0>>
<<replace "#response" t8n>>The hole cannot move any further back.<</replace>>
<<else>>
<<set _tmp to $list[$index]>>
<<set $list[$index] to $list[$index-1]>>
<<set --$index>>
<<set $list[$index] to _tmp>>
<<replace "#response">><</replace>>
<<replace "#simulation">><<include "SIMULATION">><</replace>>
<</if>><</link>>
* <<link "Undo last move">>
<<if $index lt 0>>
<<replace "#response" t8n>>We don't have a current value.<</replace>>
<<elseif $index+1 eq $sorted>>
<<replace "#response" t8n>>The hole is back where it started.<</replace>>
<<else>>
<<set _tmp to $list[$index]>>
<<set $list[$index] to $list[$index+1]>>
<<set ++$index>>
<<set $list[$index] to _tmp>>
<<replace "#response">><</replace>>
<<replace "#simulation">><<include "SIMULATION">><</replace>>
<</if>><</link>>
* <<link "Put the current value in the hole.">>
<<if $index lt 0>>
<<replace "#response" t8n>>We don't have a current value.<</replace>>
<<elseif not setup.isSorted($list.slice(0,$sorted))>>
<<replace "#response" t8n>>The current value would not be inserted correctly.<</replace>>
<<else>>
<<set $index to -1>>
<<replace "#response">><</replace>>
<<replace "#simulation">><<include "SIMULATION">><</replace>>
<</if>><</link>>
* <<link "Finish simulation">>
<<if $index gte 0>>
<<replace "#response" t8n>>We're not done with the current value.<</replace>>
<<elseif $sorted isnot $list.length>>
<<replace "#response" t8n>>We still have unsorted values.<</replace>>
<<else>>
<<goto "Done">>
<</if>><</link>>
* <<link "Shuffle and start over">>
<<run setup.shuffle($list)>>
<<set $sorted to 1>>
<<set $index to -1>>
<<replace "#response">><</replace>>
<<replace "#simulation">><<include "SIMULATION">><</replace>>
<</link>>
<<if (! $needCompletion)>>\
* [[Return to Coding|InsertionSort Method]]
<</if>>
<span id="response"></span>[[Back|Simulation]]
<<set $needCompletion to false>>\
<<if def setup.part2>><<goto "Part 2">><<else>>
<<set $code0 to $code0 + 73022>>
You have finished the simulation. Submit this code for the collaboration quiz assignment on Canvas:
<<= Math.floor($code0 / 11 + $code0 % 11)>>
<<if $name eq "guest">>''Warning'': You logged in as 'guest' and so the completion code will not work for you.<</if>>
<</if>>!! Implementation
This is the second part of the insertion sort activity, in which you will implement insertion sort on an array of comparable objects. At any point, you can return to the simulation exercise to review an example. It will let you come back here (click on "Return to Coding").
<<nobr>>
<<set $initi to setup.loopInit.length-1>>
<<set $loop1i to setup.loop1.length-1>>
<<set $loop2i to setup.loop2.length-1>>
<<set $inneri to setup.inner.length-1>>
<<set $shifti to setup.shift.length-1>>
<<set $outeri to setup.outer.length-1>>
<</nobr>>
[[Start coding|InsertionSort Method]]!! Insertion Sort Method
[[Review the simulation|Simulation]]
In this section, we develop the code for the insertion sort method. \
The code so far is given below. \
In the body of the code, click on something to change what is there.
<span id="comment"><<include "COMMENT">></span><pre>041 private void insertionSort(E[] array) {
042 int n = array.length;
043 for (int u=<<cyclearray "$initi" setup.loopInit>>; u {{{<}}} n; ++u) { {{{//}}} u = next unsorted index
044 E current = array[u];
045 int hole = u;
046 while (<<cyclearray "$loop1i" setup.loop1>> {{{&&}}} <<cyclearray "$loop2i" setup.loop2>>) {
047 <<cyclearray "$inneri" setup.inner>>;
048 <<cyclearray "$shifti" setup.shift>>;
049 }
050 <<cyclearray "$outeri" setup.outer>>;
051 }
052 }
</pre>\
<<link "Test your code">>
<<set $num to 100000 + $loop1i*10000 + $loop2i*1000 + $inneri*100 + $shifti*10 + $outeri>>
<<if $initi is setup.loopInit.length-1 or
$loop1i is setup.loop1.length-1 or
$inneri is setup.inner.length-1 or
$shifti is setup.shift.length-1 or
$outeri is setup.outer.length-1>>
<<replace "#test-error-report" t8n>>Some choices remain unchosen.<</replace>>
<<elseif $initi==2>><<goto "FailBA0">>/* 3360 cases */
<<elseif $initi==0 && $loop1i==5>><<goto "FailAA46N1">>/* 480 cases */
<<elseif $initi==0 && $loop1i==6>><<goto "FailAA46N1">>/* 480 cases */
<<elseif $loop1i==0 && $outeri==0>><<goto "FailBA0">>/* 192 cases */
<<elseif $loop1i==0 && $outeri==1>><<goto "FailBA0">>/* 192 cases */
<<elseif $loop1i==0 && $outeri==2>><<goto "FailBA0">>/* 192 cases */
<<elseif $loop1i==0 && $outeri==3>><<goto "FailAA50P2">>/* 192 cases */
<<elseif $loop1i==3 && $outeri==0>><<goto "FailBA0">>/* 192 cases */
<<elseif $loop1i==3 && $outeri==1>><<goto "FailBA0">>/* 192 cases */
<<elseif $loop1i==3 && $outeri==2>><<goto "FailBA0">>/* 192 cases */
<<elseif $loop1i==3 && $outeri==3>><<goto "FailAA50P2">>/* 192 cases */
<<elseif $loop1i==4 && $outeri==0>><<goto "FailBA0">>/* 192 cases */
<<elseif $loop1i==4 && $outeri==1>><<goto "FailBA0">>/* 192 cases */
<<elseif $loop1i==4 && $outeri==2>><<goto "FailBA0">>/* 192 cases */
<<elseif $loop1i==4 && $outeri==3>><<goto "FailAA50P2">>/* 192 cases */
<<elseif $loop1i==5 && $outeri==3>><<goto "FailAA50P2">>/* 96 cases */
<<elseif $loop1i==5 && $outeri==4>><<goto "FailAB1">>/* 96 cases */
<<elseif $loop1i==6 && $outeri==0>><<goto "FailBA0">>/* 96 cases */
<<elseif $loop1i==6 && $outeri==1>><<goto "FailBA0">>/* 96 cases */
<<elseif $loop1i==6 && $outeri==2>><<goto "FailBA0">>/* 96 cases */
<<elseif $loop1i==6 && $outeri==3>><<goto "FailAA50P2">>/* 96 cases */
<<elseif $loop1i==6>><<goto "FailBA0">>/* 96 cases */
<<elseif $initi==0 && $loop1i==0>><<goto "FailAA50N1">>/* 96 cases */
<<elseif $loop1i==0>><<goto "FailAB1">>/* 96 cases */
<<elseif $initi==0 && $loop1i==3>><<goto "FailAA50N1">>/* 96 cases */
<<elseif $loop1i==3>><<goto "FailAB1">>/* 96 cases */
<<elseif $initi==0 && $loop1i==4>><<goto "FailAA50N1">>/* 96 cases */
<<elseif $loop1i==4>><<goto "FailAB1">>/* 96 cases */
<<elseif $loop2i==0 && $outeri==0>><<goto "FailBA0">>/* 60 cases */
<<elseif $loop2i==0 && $outeri==1>><<goto "FailBA0">>/* 60 cases */
<<elseif $loop2i==0 && $outeri==2>><<goto "FailBA0">>/* 60 cases */
<<elseif $loop2i==3 && $outeri==0>><<goto "FailBA0">>/* 60 cases */
<<elseif $loop2i==3 && $outeri==1>><<goto "FailBA0">>/* 60 cases */
<<elseif $loop2i==3 && $outeri==2>><<goto "FailBA0">>/* 60 cases */
<<elseif $loop2i==4 && $outeri==0>><<goto "FailBA0">>/* 60 cases */
<<elseif $loop2i==4 && $outeri==1>><<goto "FailBA0">>/* 60 cases */
<<elseif $loop2i==4 && $outeri==2>><<goto "FailBA0">>/* 60 cases */
<<elseif $loop2i==0 && $outeri==3>><<goto "FailAA50P2">>/* 48 cases */
<<elseif $loop2i==3 && $outeri==3>><<goto "FailAA50P2">>/* 48 cases */
<<elseif $loop2i==4 && $outeri==3>><<goto "FailAA50P2">>/* 48 cases */
<<elseif $loop1i==5 && $loop2i==6>><<goto "FailBA0">>/* 36 cases */
<<elseif $loop1i==5 && $shifti==0>><<goto "FailBA46N1">>/* 72 cases */
<<elseif $initi==0 && $loop2i==0>><<goto "FailAA50N1">>/* 24 cases */
<<elseif $loop2i==0>><<goto "FailAB1">>/* 24 cases */
<<elseif $initi==0 && $loop2i==3>><<goto "FailAA50N1">>/* 24 cases */
<<elseif $loop2i==3>><<goto "FailAB1">>/* 24 cases */
<<elseif $initi==0 && $loop2i==4>><<goto "FailAA50N1">>/* 24 cases */
<<elseif $loop2i==4>><<goto "FailAB1">>/* 24 cases */
<<elseif $loop1i==5 && $inneri==0>><<goto "FailBAA0">>/* 12 cases */
<<elseif $loop1i==5 && $inneri==1>><<goto "FailBAA0">>/* 12 cases */
<<elseif $loop1i==5 && $inneri==2>><<goto "FailBAA0">>/* 12 cases */
<<elseif $loop1i==5 && $inneri==3>><<goto "FailBAA0">>/* 12 cases */
<<elseif $loop1i==5 && $inneri==4>><<goto "FailBA47P2">>/* 12 cases */
<<elseif $loop1i==5>><<goto "FailBA46P2">>/* 12 cases */
<<elseif $loop1i==1 && $shifti==0 && $outeri==0>><<goto "FailBA0">>/* 60 cases */
<<elseif $loop1i==1 && $shifti==0 && $outeri==2>><<goto "FailBA0">>/* 60 cases */
<<elseif $loop1i==1 && $shifti==0 && $outeri==4>><<goto "FailBAC50N1">>/* 60 cases */
<<elseif $loop1i==2 && $loop2i==5 && $shifti==0>><<goto "FailBA46N1">>/* 60 cases */
<<elseif $loop1i==2 && $loop2i==6 && $shifti==0>><<goto "FailAB46N1">>/* 60 cases */
<<elseif $initi==0 && $loop1i==1 && $outeri==4>><<goto "FailAA50N1">>/* 30 cases */
<<elseif $initi==0 && $loop1i==2 && $loop2i==5>><<goto "FailAA46N1">>/* 30 cases */
<<elseif $loop2i==5 && $outeri==3>><<goto "FailAA50P2">>/* 30 cases */
<<elseif $loop2i==5 && $outeri==4>><<goto "FailAB1">>/* 12 cases */
<<elseif $initi==1 && $inneri==4 && $shifti==1>><<goto "FailABA47P3">>/* 46 cases */
<<elseif $initi==0 && $loop1i==2 && $loop2i==6>><<goto "FailAA46N1">>/* 30 cases */
<<elseif $loop2i==6 && $outeri==1>><<goto "FailBA0">>/* 28 cases */
<<elseif $loop2i==6 && $outeri==3>><<goto "FailAA50P2">>/* 28 cases */
<<elseif $loop2i==6>><<goto "FailBA0">>/* 42 cases */
<<elseif $loop1i==1 && $shifti==0 && $outeri==3>><<goto "FailAB1">>/* 36 cases */
<<elseif $loop2i==1 && $shifti==0 && $outeri==1>><<goto "FailAB0">>/* 24 cases */
<<elseif $loop2i==2 && $inneri==0 && $shifti==1>><<goto "FailAA">>/* 19 cases */
<<elseif $loop2i==2 && $inneri==1 && $shifti==1>><<goto "FailAA">>/* 19 cases */
<<elseif $loop1i==1 && $loop2i==2 && $shifti==1>><<goto "FailAA47P2">>/* 31 cases */
<<elseif $loop1i==1 && $loop2i==2>><<goto "FailAB0">>/* 12 cases */
<<elseif $loop2i==2 && $inneri==4>><<goto "FailAA47N1">>/* 15 cases */
<<elseif $loop2i==7 && $inneri==0 && $shifti==1>><<goto "FailAA">>/* 19 cases */
<<elseif $loop2i==7 && $inneri==1 && $shifti==1>><<goto "FailAA">>/* 19 cases */
<<elseif $loop1i==1 && $loop2i==7 && $shifti==1>><<goto "FailAA47P2">>/* 31 cases */
<<elseif $loop1i==1 && $loop2i==7>><<goto "FailAB0">>/* 12 cases */
<<elseif $loop2i==7 && $inneri==4>><<goto "FailAA47N1">>/* 15 cases */
<<elseif $inneri==4 && $outeri==4>><<goto "FailAA50N1">>/* 3 cases */
<<elseif $inneri==4 && $shifti==1>><<goto "FailBA47P2">>/* 11 cases */
<<elseif $loop2i==5 && $outeri==2>><<goto "FailCBA50P3">>/* 15 cases */
<<elseif $loop2i==1 && $inneri==4>><<goto "FailAB1">>/* 6 cases */
<<elseif $loop2i==5 && $shifti==1 && $outeri==1>><<goto "FailCBBA50P4">>/* 15 cases */
<<elseif $initi==1 && $loop2i==2 && $shifti==1>><<goto "FailAA47P2">>/* 15 cases */
<<elseif $loop2i==2 && $inneri==5>><<goto "FailAA47N1">>/* 15 cases */
<<elseif $loop2i==2 && $shifti==1>><<goto "FailAA47P2">>/* 10 cases */
<<elseif $loop2i==2 && $outeri==0>><<goto "FailBAB0">>/* 8 cases */
<<elseif $loop2i==2>><<goto "FailAA50N1">>/* 32 cases */
<<elseif $initi==1 && $loop2i==7 && $shifti==1>><<goto "FailAA47P2">>/* 15 cases */
<<elseif $loop2i==7 && $inneri==5>><<goto "FailAA47N1">>/* 15 cases */
<<elseif $shifti==0 && $outeri==0>><<goto "FailBAB0">>/* 18 cases */
<<elseif $loop2i==7 && $shifti==0>><<goto "FailAA50N1">>/* 32 cases */
<<elseif $loop2i==7>><<goto "FailAA47P2">>/* 10 cases */
<<elseif $shifti==0 && $outeri==2>><<goto "FailBA0">>/* 10 cases */
<<elseif $shifti==0 && $outeri==3>><<goto "FailAB1">>/* 10 cases */
<<elseif $shifti==0 && $outeri==4>><<goto "FailAA50N1">>/* 10 cases */
<<elseif $initi==0 && $outeri==4>><<goto "FailAA50N1">>/* 5 cases */
<<elseif $loop2i==1 && $inneri==0>><<goto "FailAA">>/* 18 cases */
<<elseif $loop2i==1 && $inneri==1>><<goto "FailAA">>/* 18 cases */
<<elseif $loop2i==1>><<goto "FailAA47P2">>/* 54 cases */
<<elseif $inneri==0 && $outeri==0>><<goto "FailBA0">>/* 3 cases */
<<elseif $inneri==0>><<goto "FailBA1">>/* 2 cases */
<<elseif $inneri==1 && $outeri==0>><<goto "FailBA0">>/* 3 cases */
<<elseif $inneri==1>><<goto "FailBA1">>/* 2 cases */
<<elseif $inneri==2 && $outeri==0>><<goto "FailBA0">>/* 3 cases */
<<elseif $inneri==2>><<goto "FailBA1">>/* 2 cases */
<<elseif $inneri==3 && $outeri==0>><<goto "FailBA0">>/* 3 cases */
<<elseif $outeri==0>><<goto "FailBA46P2">>/* 3 cases */
<<elseif $inneri==3>><<goto "FailBA1">>/* 2 cases */
<<elseif $inneri==5>><<goto "FailBCA1">>/* 2 cases */
<<else>><<goto "Success">> /* 2 cases */
<</if>>
<</link>>
<span id="test-error-report"></span><<if ndef $comment>><<set $comment to false>><</if>>\
<<if $comment>>\
<<link "⊖">><<set $comment to false>><<replace "#comment">><<include "COMMENT">><</replace>><</link>>\
<tt>36 {{{/**}}}</tt>\
<pre>037 * Sort an array using insertion sort.
038 * The elements must be comparable, and are sorted in ascending order.
039 * @param source array to operate on
040 */</pre>\
<<else>><<link "⊕">><<set $comment to true>><<replace "#comment">><<include "COMMENT">><</replace>><</link>>\
<tt>36 {{{/**}}}</tt><</if>>\!! Nontermination when running <code>testAA</code>
When we test the code with <code>testAA</code>, \
the code gets stuck in an infinite loop (the inner loop starting on line 46 loops forever):
{{{
public void testAA() {
String[] source = new String[]{"A","A"};
SortedCollection.insertionSort(source); // loops while running the algorithm
assertEquals("A",source[0]);
assertEquals("A",source[1]);
}
}}}
[[Return|InsertionSort Method]]!! Array error when running <code>testAA</code>
When we test the code with <code>testAA</code>, \
the code gets an {{{ArrayIndexOutOfBoundsException(-1)}}} on line 46 (the inner loop header):
{{{
public void testAA() {
String[] source = new String[]{"A","A"};
SortedCollection.insertionSort(source); // crashed while running the algorithm
assertEquals("A",source[0]);
assertEquals("A",source[1]);
}
}}}
[[Return|InsertionSort Method]]!! Array error when running <code>testAA</code>
When we test the code with <code>testAA</code>, \
the code gets an {{{ArrayIndexOutOfBoundsException(-1)}}} on line 47 (the first line in the inner loop body):
{{{
public void testAA() {
String[] source = new String[]{"A","A"};
SortedCollection.insertionSort(source); // crashed while running the algorithm
assertEquals("A",source[0]);
assertEquals("A",source[1]);
}
}}}
[[Return|InsertionSort Method]]!! Array error when running <code>testAA</code>
When we test the code with <code>testAA</code>, \
the code gets an {{{ArrayIndexOutOfBoundsException(2)}}} on line 47 (the first line in the inner loop body):
{{{
public void testAA() {
String[] source = new String[]{"A","A"};
SortedCollection.insertionSort(source); // crashed while running the algorithm
assertEquals("A",source[0]);
assertEquals("A",source[1]);
}
}}}
[[Return|InsertionSort Method]]!! Array error when running <code>testAA</code>
When we test the code with <code>testAA</code>, \
the code gets an {{{ArrayIndexOutOfBoundsException(-1)}}} on line 50 (the last line in the {{for}} loop body):
{{{
public void testAA() {
String[] source = new String[]{"A","A"};
SortedCollection.insertionSort(source); // crashed while running the algorithm
assertEquals("A",source[0]);
assertEquals("A",source[1]);
}
}}}
[[Return|InsertionSort Method]]!! Array error when running <code>testAA</code>
When we test the code with <code>testAA</code>, \
the code gets an {{{ArrayIndexOutOfBoundsException(2)}}} on line 50 (the last line in the {{for}} loop body):
{{{
public void testAA() {
String[] source = new String[]{"A","A"};
SortedCollection.insertionSort(source); // crashed while running the algorithm
assertEquals("A",source[0]);
assertEquals("A",source[1]);
}
}}}
[[Return|InsertionSort Method]]!! Assertion failure when running <code>testAB</code>
When we test the code with <code>testAB</code>, \
the first assertion fails (the first element was "B", not "A"):
{{{
public void testAB() {
String[] source = new String[]{"A","B"};
SortedCollection.insertionSort(source);
assertEquals("A",source[0]); // This assertion fails
assertEquals("B",source[1]);
}
}}}
[[Return|InsertionSort Method]]!! Assertion failure when running <code>testAB</code>
When we test the code with <code>testAB</code>, \
the second assertion fails (the first element was "A", not "B"):
{{{
public void testAB() {
String[] source = new String[]{"A","B"};
SortedCollection.insertionSort(source);
assertEquals("A",source[0]);
assertEquals("B",source[1]); // This assertion fails
}
}}}
[[Return|InsertionSort Method]]!! Array error when running <code>testAB</code>
When we test the code with <code>testAB</code>, \
the code gets an {{{ArrayIndexOutOfBoundsException(-1)}}} on line 46 (the inner loop header):
{{{
public void testAB() {
String[] source = new String[]{"A","B"};
SortedCollection.insertionSort(source); // crashed while running the algorithm
assertEquals("A",source[0]);
assertEquals("B",source[1]);
}
}}}
[[Return|InsertionSort Method]]!! Array error when running <code>testABA</code>
When we test the code with <code>testABA</code>, \
the code gets an {{{ArrayIndexOutOfBoundsException(3)}}} on line 47 (the first line in the inner loop body):
{{{
public void testABA() {
String[] source = new String[]{"A","B","A"};
SortedCollection.insertionSort(source); // crashed while running the algorithm
assertEquals("A",source[0]);
assertEquals("A",source[1]);
assertEquals("B",source[2]);
}
}}}
[[Return|InsertionSort Method]]!! Assertion failure when running <code>testBA</code>
When we test the code with <code>testBA</code>, \
the first assertion fails (the first element was "B", not "A"):
{{{
public void testBA() {
String[] source = new String[]{"B","A"};
SortedCollection.insertionSort(source);
assertEquals("A",source[0]); // This assertion fails
assertEquals("B",source[1]);
}
}}}
[[Return|InsertionSort Method]]!! Assertion failure when running <code>testBA</code>
When we test the code with <code>testBA</code>, \
the second assertion fails (the second element was "A", not "B"):
{{{
public void testBA() {
String[] source = new String[]{"B","A"};
SortedCollection.insertionSort(source);
assertEquals("A",source[0]);
assertEquals("B",source[1]); // This assertion fails
}
}}}
[[Return|InsertionSort Method]]!! Array error when running <code>testBA</code>
When we test the code with <code>testBA</code>, \
the code gets an {{{ArrayIndexOutOfBoundsException(-1)}}} on line 46 (the inner loop header):
{{{
public void testBA() {
String[] source = new String[]{"B","A"};
SortedCollection.insertionSort(source); // crashed while running the algorithm
assertEquals("A",source[0]);
assertEquals("B",source[1]);
}
}}}
[[Return|InsertionSort Method]]!! Array error when running <code>testBA</code>
When we test the code with <code>testBA</code>, \
the code gets an {{{ArrayIndexOutOfBoundsException(2)}}} on line 46 (the inner loop header):
{{{
public void testBA() {
String[] source = new String[]{"B","A"};
SortedCollection.insertionSort(source); // crashed while running the algorithm
assertEquals("A",source[0]);
assertEquals("B",source[1]);
}
}}}
[[Return|InsertionSort Method]]!! Array error when running <code>testBA</code>
When we test the code with <code>testBA</code>, \
the code gets an {{{ArrayIndexOutOfBoundsException(2)}}} on line 47 (the first line in the inner loop body):
{{{
public void testBA() {
String[] source = new String[]{"B","A"};
SortedCollection.insertionSort(source); // crashed while running the algorithm
assertEquals("A",source[0]);
assertEquals("B",source[1]);
}
}}}
[[Return|InsertionSort Method]]!! Assertion failure when running <code>testBAA</code>
When we test the code with <code>testBAA</code>, \
the first assertion fails (the first element was "B", not "A"):
{{{
public void testBA() {
String[] source = new String[]{"B","A","A"};
SortedCollection.insertionSort(source);
assertEquals("A",source[0]); // This assertion fails
assertEquals("A",source[1]);
assertEquals("B",source[2]);
}
}}}
[[Return|InsertionSort Method]]!! Assertion failure when running <code>testBAB</code>
When we test the code with <code>testBAB</code>, \
the first assertion fails (the first element was "B", not "A"):
{{{
public void testBA() {
String[] source = new String[]{"B","A","B"};
SortedCollection.insertionSort(source);
assertEquals("A",source[0]); // This assertion fails
assertEquals("B",source[1]);
assertEquals("B",source[2]);
}
}}}
[[Return|InsertionSort Method]]!! Array error when running <code>testBAC</code>
When we test the code with <code>testBAC</code>, \
the code gets an {{{ArrayIndexOutOfBoundsException(-1)}}} on line 50 (the last line in the {{for}} loop body):
{{{
public void testBAC() {
String[] source = new String[]{"B","A","C"};
SortedCollection.insertionSort(source); // crashed while running the algorithm
assertEquals("A",source[0]);
assertEquals("B",source[1]);
assertEquals("C",source[2]);
}
}}}
[[Return|InsertionSort Method]]!! Assertion failure when running <code>testBCA</code>
When we test the code with <code>testBCA</code>, \
the second assertion fails (the second element was "C", not "B"):
{{{
public void testBCA() {
String[] source = new String[]{"B","C","A"};
SortedCollection.insertionSort(source);
assertEquals("A",source[0]);
assertEquals("B",source[1]); // This assertion fails
assertEquals("C",source[2]);
}
}}}
[[Return|InsertionSort Method]]!! Array error when running <code>testCBA</code>
When we test the code with <code>testCBA</code>, \
the code gets an {{{ArrayIndexOutOfBoundsException(3)}}} on line 50 (the last line in the {{for}} loop body):
{{{
public void testCBA() {
String[] source = new String[]{"C","B","A"};
SortedCollection.insertionSort(source); // crashed while running the algorithm
assertEquals("A",source[0]);
assertEquals("B",source[1]);
assertEquals("C",source[2]);
}
}}}
[[Return|InsertionSort Method]]!! Array error when running <code>testCBBA</code>
When we test the code with <code>testCBBA</code>, \
the code gets an {{{ArrayIndexOutOfBoundsException(4)}}} on line 50 (the last line in the {{for}} loop body):
{{{
public void testCBA() {
String[] source = new String[]{"C","B","B","A"};
SortedCollection.insertionSort(source); // crashed while running the algorithm
assertEquals("A",source[0]);
assertEquals("B",source[1]);
assertEquals("B",source[2]);
assertEquals("C",source[3]);
}
}}}
[[Return|InsertionSort Method]][[Back|InsertionSort Method]]
!! Success
You have correctly completed the implementation of insertion sort. \
Please turn in this code to show your completion: <<=jtb.comb3($num,41,$code0)>>! Log in
(We will not ask for a password.)
<<nobr>>
<<set $code0 to 0>>
<</nobr>>
EPantherID: <<textboxplus "$name" "guest" autofocus>>
<<set $code0 to jtb.icHash($name+setup.semester)>>
<<set $badchars to jtb.badLogin($name)>>
<<if setup.logins.includes($name)>><<goto "LoggedIn">><<else>>
<<replace "#login-error">><<if $badchars isnot "">>
An EPantherID cannot include characters such as $badchars.<<else>>
The EPantherID $name is not registered with the class.<</if>>
[[Proceed without credit->LoggedIn]]<</replace>><</if>>
<</textboxplus>>@uwm.edu
<span id="login-error"></span>