Java: Change InsertionSort class to sort an array of double elements, rather tha
ID: 3775497 • Letter: J
Question
Java:
Change InsertionSort class to sort an array of double elements, rather than array of char elements.
In the Lab, you use a single System.out.println() statement to print an array of characters by using a constructor in the String class which takes the char array test:
System.out.println("Unsorted: " + new String(test)) ;
Unfortunately, this won’t work with an array of double elements. Write your own method which takes an array of double elements as a parameter and prints them on a single line on the console (separated by a space).
InsertionSort class:
public class InsertionSort
{
public static void main(String args[])
{
char[] test = new char[] { 'm', 'X', '%', 'B', 'e', '!' };
System.out.println("Unsorted: " + new String(test)) ;
sort(test) ;
System.out.println("Sorted: " + new String(test)) ;
}
public static void sort(char[] a)
{
// Create a temporary array to insert characters sorted
char[] temp = new char[a.length] ;
// Loop through remaining chars, inserting them into temp[]
for (int index = 0 ; index < a.length ; index++)
{
// Find the appropriate spot in temp[] to insert a[index]
int insertHere = 0 ;
insertHere = tempSort(a, temp, index, insertHere);
// in reverse order
moveArray(temp, index, insertHere);
// Insert the character from a[] into its sorted spot
insertValue(a, temp, index, insertHere);
System.out.print("When index = " + index + ": ") ;
for (int i = 0 ; i <= index ; i++)
{
System.out.print(temp[i]) ;
}
System.out.println() ;
} // end of for loop
// All done sorting, so copy the temp[] array back into a[]
copyArray(a, temp);
} // end of sort() method
//find the insertion index for a[index] in temp
private static int tempSort(char[] a, char[] temp, int index, int insertHere)
{
while ((insertHere < index) && (temp[insertHere] < a[index]))
{
insertHere++ ;
}
return insertHere;
}
//move characters down one position in temp
private static void moveArray(char[] temp, int index, int insertHere)
{
for (int i = index - 1 ; i >= insertHere ; i--)
{
temp[i + 1] = temp[i] ;
}
}
//insert a[index] into temp[]
private static void insertValue(char[] a, char[] temp, int index, int insertHere)
{
temp[insertHere] = a[index] ;
}
//copy the temp array to a
private static void copyArray(char[] a, char[] temp)
{
for (int index = 0 ; index < a.length ; index++)
{
insertValue(temp, a, index, index);
}
}
}
Explanation / Answer
import java.util.Arrays;
public class InsertionSort
{
public static void main(String args[])
{
double[] test = new double[] { 1.2,5.4,3.7,4.6,2.4 };
System.out.println("Unsorted Array is : "+Arrays.toString(test));
sort(test) ;
}
public static void sort(double[] test)
{
// Create a temporary array to insert sorted elements
double[] temp = new double[test.length] ;
int i;
// Loop through remaining elements, inserting them into temp[]
for (int index = 0 ; index < test.length ; index++)
{
// Find the appropriate spot in temp[] to insert a[index]
int insertHere = 0 ;
insertHere = tempSort(test, temp, index, insertHere);
// in reverse order
moveArray(temp, index, insertHere);
// Insert the element from a[] into its sorted spot
insertValue(test, temp, index, insertHere);
System.out.print("When index = " + index + ": ") ;
for (i = 0 ; i <= index ; i++)
{
System.out.print(temp[i]+" ") ;
}
System.out.println() ;
} // end of for loop
// All done sorting, so copy the temp[] array back into a[]
copyArray(test, temp);
} // end of sort() method
//find the insertion index for a[index] in temp
private static int tempSort(double[] test, double[] temp, int index, int insertHere)
{
while ((insertHere < index) && (temp[insertHere] < test[index]))
{
insertHere++ ;
}
return insertHere;
}
//move element down one position in temp
private static void moveArray(double[] temp, int index, int insertHere)
{
for (int i = index - 1 ; i >= insertHere ; i--)
{
temp[i + 1] = temp[i] ;
}
}
//insert a[index] into temp[]
private static void insertValue(double[] test, double[] temp, int index, int insertHere)
{
temp[insertHere] = (char) test[index] ;
}
//copy the temp array to a
private static void copyArray(double[] test, double[] temp)
{
for (int index = 0 ; index < test.length ; index++)
{
insertValue(test, temp, index, index);
}
}
}
Outputs:
Unsorted Array is : [1.2, 5.4, 3.7, 4.6, 2.4]
When index = 0: 1.0
When index = 1: 1.0 5.0
When index = 2: 1.0 3.0 5.0
When index = 3: 1.0 3.0 4.0 5.0
When index = 4: 1.0 2.0 3.0 4.0 5.0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.