Question: My library > COSC 2 2 0 : Data Structures and Algorithms home > 3 . 1 0 : LAB: Insertion sort zyBooks catalog 3

My library > COSC 220: Data Structures and Algorithms home >3.10: LAB: Insertion sort
zyBooks catalog
3.10 LAB: Insertion sort
The program has four steps:
Read the size of an integer array, followed by the elements of the array (no duplicates).
Output the array.
Perform an insertion sort on the array.
Output the number of comparisons and swaps performed.
main0 performs steps 1 and 2.
Implement step 3 based on the insertion sort algorithm in the book. Modify insertionSort 0 to:
Count the number of comparisons performed.
Count the number of swaps performed.
Output the array during each iteration of the outside loop.
Complete main() to perform step 4, according to the format shown in the example below.
Hints: In order to count comparisons and swaps, modify the while loop in insertionSort(0. Use static variables for comparisons and swaps.
The program provides three helper methods:Ex: When the input is:ks My library > COSC 220: Data Structures and Algorithms home >3.10: LAB: Insertion sort
zyBooks catalog
void swap(int[] nums, int j, int k)
Ex: When the input is:
6,3,2,1,5,9,8
the output is:
\table[[3,2,1,5,9,8],[2,3,1,5,9,8],[1,2,3,5,9,8],[1,2,3,5,9,8],[1,2,3,5,9,8],[1,2,3,5,8,9]]
573528.4271126.qx3zqy7
LAB
ACTIVITY
3.10.1: LAB: Insertion sort
010
LabProgram.java
Load default template...
\table[[66],[67],[68],[69],[70],[71],[72],[73],[74],[75]]
// Step 2: Output the numbers array printNums(numbers);
System.out.println();
// Step 3: Sort the numbers array using insertion sort insertionSort(numbers);
System.out.println();
// Step 4: Output the number of comparisons and swaps performedzyBooks My library > COSC 220: Data Structures and Algorithms home >3.10 : LAB: Insertion sort
zyBooks catalog
The program has four steps:
Read the size of an integer array, followed by the elements of the array (no duplicates).
Output the array.
Perform an insertion sort on the array.
Output the number of comparisons and swaps performed.
main( performs steps 1 and 2.
Implement step 3 based on the insertion sort algorithm in the book. Modify insertionSort0 to:
Count the number of comparisons performed.
Count the number of swaps performed.
Output the array during each iteration of the outside loop.
Complete main() to perform step 4, according to the format shown in the example below.
Hints: In order to count comparisons and swaps, modify the while loop in insertionSort0. Use static variables for comparisons and swaps The program provides three helper
methods:Ex:When the input is:
6321598
the output is:
321598
231598
123598
123598
123598
123589
comparisons: 7
swaps: 4
 My library > COSC 220: Data Structures and Algorithms home >3.10:

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!