Question: Modify the code below to count the number of comparisons and the number of swaps import java.util.Arrays; import java.util.Random; public class InsertionSort { private int[]

Modify the code below to count the number of comparisons and the number of swaps

import java.util.Arrays; import java.util.Random;

public class InsertionSort { private int[] data; private static final Random generator = new Random();

public InsertionSort( int size ) { data = new int[ size ];

for ( int i = 0; i < size; i++ ) data[ i ] = 10 + generator.nextInt( 90 ); }

// call this method from main program public void sort(){ int insert;

for ( int next = 1; next < data.length; next++ ) { insert = data[ next ]; int moveItem = next;

while ( moveItem > 0 && data[ moveItem - 1 ] > insert ) { data[ moveItem ] = data[ moveItem - 1 ]; moveItem--; }

data[ moveItem ] = insert; } } }

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!