Copy and paste the SelectionSortTester code from our lesson into BlueJ public class SelectionSortTester { // Sort
Question:
- Copy and paste the SelectionSortTester code from our lesson into BlueJ
public class SelectionSortTester
{
// Sort the Array
//
public static void selectionSort( int[] array )
{
// Find the integer that should go in each cell of
// the array, from cell 0 to the end
for ( int j=0; j
{
// Find min: the index of the integer that should go into cell j.
// Look through the unsorted integers (those at j or higher)
int min = j;
for ( int k=j+1; k
{
if ( array[k] < array[min] )
{
min = k;
}
}
// Swap the int at j with the int at min
int temp = array[min];
array[min] = array[j];
array[j] = temp;
}
}
public static void main ( String[] args )
{
int[] values = { 17, 5, 21, 8, 19, 2, 23, 15, 4, 13 };
// print out the array
System.out.println("initial values: ");
for ( int val : values )
{
System.out.print( val + ", " );
}
// sort the array
selectionSort( values );
// print out the array
System.out.println(" sorted values: ");
for ( int val : values )
{
System.out.print( val + ", " );
}
System.out.println( );
}
}
2.Modify the programSelectionSortTester so that the array is initialized to SIZE number of random integers. Ask the user for SIZE. If SIZE is large it is not useful to print out the array, so don't print out the array but include theisSorted() method to say if the array has been sorted.
size of the array -->12000 Before:NOT sorted After:SORTED
3.Further modify the program so that it determines the time it takes to sort the array:
long startTime = System.currentTimeMillis(); selectionSort( values ); long endTime = System.currentTimeMillis(); System.out.println("Total execution time: " + (endTime - startTime) );
This is more accurate than timing the program with a clock.
4. Further modify the program so it creates a random array, sorts it, prints out the time it takes to sort, and then sorts the array a second time (so it sorts an already sorted array) and prints out the time the second sort took.
Observe the two times. Explain your observation.
5. Modify the program so that it asks the user for the size of the array, initializes it to random integers, sorts the array, and then counts the number of duplicates.
Could someone please help me out with this?
Java How To Program Late Objects Version
ISBN: 9780136123712
8th Edition
Authors: Paul Deitel, Deitel & Associates