Question: Add a method insertionSort to the class ArraySorter, as given in Listing 7.10, that performs an insertion sort of an array. To simplify this project,
Add a method insertionSort to the class ArraySorter, as given in Listing 7.10, that performs an insertion sort of an array. To simplify this project, our insertion sort algorithm will use an additional array. It copies elements from the original given array to this other array, inserting each element into its correct position in the second array. This will usually require moving a number of elements in the array receiving the new elements. The algorithm in pseudocode is as follows: Insertion sort algorithm to sort an array a for (index = 0; index < a.length; index++) Insert the value of a[index] into its correct position in the array temp, so that all the elements copied into the array temp so far are sorted. Copy all the elements from temp back to a. The array temp is partially filled and is a local variable in the method sort. Notes: 1. This project also requires an extension of the algorithm in the problem statement. In these descriptions the word "up" is used to mean a higher subscript in the array. (It could just as easily be called "down;" the important thing is to use the directions consistently in all descriptions.) The sorted array, temp, is created one element at a time, then, when all elements have been inserted correctly, temp needs to be copied back into the original array: For each element in the original array: { Get next value. Find its insertion point: { Compare next value to each element of temp, in order, starting at the lowest. The insertion point is found either when next value > element in temp, or the end of temp is reached. } Starting at the end of temp and working backward through the insertion point, move the elements in temp up one place. (You need to start at the top and work backward to avoid overwriting data in temp, and the value at the insertion point needs to be moved so next value can be inserted.) Insert next value into temp at the insertion point. } Copy temp array into original array: the original array is now sorted. Following the approach in the text, an additional class, InsertionSortDemo, is used to demonstrate the bubble sort method with a sample array.
I have written the code below but it won't run on java eclipse please help cant figure out the error (possibly missing a main method)
public class ArraySorter
{
public static void insertionSort(int[] a)
{
// create a temporary array named "temp" of size as array a //
int[] temp = new int[a.length];
int index;
int index1, index2, value;
//copy each integer of the array a into its corresponding position in the array temp
for(index = 0; index < a.length; index++)
temp[index] = a[index];
// repeat the loop until all integers to be sorted
for(index1 = 1; index1 < temp.length; index1++)
{
// get the integer at the position index1 of the array temp and assign it into the variable value
value = temp[index1];
// compare the value with previous integer, move the value to the previous position if the integer is
//greater than the value, repeat this process until the value reaches its correct position
for(index2 = index1; (index2 > 0) &&
(temp[index2 - 1]) > value; index2--)
temp[index2] = temp[index2 - 1];
//assign the value to the position index2 in the array temp
temp[index2] = value;
}//end for
/* copy each integer of the array temp into its
* corresponding position in the array a */
for(index = 0; index < temp.length; index++)
a[index] = temp[index];
}// end of insertion method
/* this method sorts an array of integers
in non descending order */
public static void selectionSort (int[] anArray)
{
for(int index = 0; index < anArray.length - 1;
index++)
{
// call the interchange method to swap the integers in the array
int indexOfnextSmallest =
getIndexOfSmallest(index, anArray);
interchange(index, indexOfnextSmallest, anArray);
}
}
private static int getIndexOfSmallest (int startIndex, int [ ] a)
{
//get the first integer as minimum
int min = a[startIndex];
// get the first index as the index of the minimum integer
int indexOfMin = startIndex;
// repeat the loop from, the second integer to the last integer of the array
for(int index = startIndex +1; index < a.length; index++)
{
// verify integer at index if it is less than minimum
if(a[index]< min)
{
//change minimum integer
min = a[index];
//change index of minimum integer
indexOfMin = index;
indexOfMin = index;
}// end if
}//end for
// return the index of minimum integer
return indexOfMin;
}
private static void interchange( int i, int j, int[] a)
{
/* change the first integer as the temporary
integer */
int temp = a[i];
// change the second value as the first integer
a[i] = a[j];
//change the second value as the first integer
// change the temporary integer as the second integer
a[j] = temp;
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
