Hey guys. I am having problems with this code. I am somewhat new to java so this may be a simple mistake on my part. Any help or direction would be greatly appreciated.
I am trying to convert this code to use double instead of int. It is a insertion sort program that sorts the data from a random number generator. I am having issues with the constructor InsertionSort. It warns me that it requires type int and found type double so there will be loss of precision. I do not know why it requires type int. The argument is type double so shouldn't it require type double? Am I missing where int is declared? Thanks in advance for any help!
import java.util.Random;
import java.lang.*;
public class InsertionSort
{
private double[] data; // array of values
private static Random generator = new Random();
// create array of given size and fill with random integers
public InsertionSort( double size )
{
data = new double [ size ]; // create space for array
// fill array with random ints in range 10-99
for ( double i = 0; i < size; i++ )
data[ i ] = 10.00 + generator.nextdouble( 90 );
} // end InsertionSort constructor
// sort array using insertion sort
public void sort()
{
double insert; // temporary variable to hold element to insert
// loop over data.length - 1 elements
for ( double next = 1; next < data.length; next++ )
{
// store value in current element
insert = data[ next ];
// initialize location to place element
double moveItem = next;
// search for place to put current element
while ( moveItem > 0 && data[ moveItem - 1 ] > insert )
{
// shift element right one slot
data[ moveItem ] = data[ moveItem - 1 ];
moveItem--;
} // end while
data[ moveItem ] = insert; // place inserted element
printPass( next, moveItem ); // output pass of algorithm
} // end for
} // end method sort
// print a pass of the algorithm
public void printPass( double pass, double index )
{
System.out.print( String.format( "after pass %2d: ", pass ) );
// output elements till swapped item
for ( int i = 0 ; i < index; i++ )
System.out.print( data[ i ] + " " );
System.out.print( data[ index ] + "* " ); // indicate swap
// finish outputting array
for ( int i = index + 1; i < data.length; i++ )
System.out.print( data[ i ] + " " );
System.out.print( "\n " ); // for alignment
// indicate amount of array that is sorted
for( int i = 0; i <= pass; i++ )
System.out.print( "-- " );
System.out.println( "\n" ); // add endline
} // end method printPass
// method to output values in array
public String toString()
{
StringBuffer temporary = new StringBuffer();
// iterate through array
for ( double element : data )
temporary.append( element + " " );
temporary.append( "\n" ); // add endline character
return temporary.toString();
} // end method toString
} // end class InsertionSort
public class InsertionSortTest
{
public static void main( String[] args )
{
// create object to perform selection sort
InsertionSort sortArray = new InsertionSort( 10.00 );
System.out.println( "Unsorted array:" );
System.out.println( sortArray ); // print unsorted array
sortArray.sort(); // sort array
System.out.println( "Sorted array:" );
System.out.println( sortArray ); // print sorted array
} // end main
} // end class InsertionSortTest