Question

// ArrayIns.java // demonstrates insertion sort 11--- class ArrayIns private long[] a; private int nElems; // ref to array a

V/ InsertSortTest.java // Test the insert sort algorithm -- class InsertSortTest public static void main(String[] args) { int

Description of the Problem: ArrayIns.java is the source code for the insertion sort algorithm, it is based on the process of

0 0
Add a comment Improve this question Transcribed image text
Answer #1

In the provided code there is ambiguity in the insertionSort() method. After the while iteration the value of a[in] is not updated with the temp stored in each for iteration. A detailed explanation is provided below.

After each iteration of for loop the array elements to the left of the element in consideration is sorted. In doing so the element in consideration is inserted in the right location with respect to the elements to its left, like inserting card in ordered manner. In the above code the insertion part of the element to its correct place is missing.

In the pseudocode mentioned above there is a logical error in the program. The contents inside the while loop of the code will never be executed as for each for iteration the value of A [j] is stored to key, value of j is stored to i   ( i = j ) and then A[j] is compared to key (A [i] > key). As key, A[i] & A[j] stores the same value therefor the comparison is always false. So the algorithm will not operate. In the condition statement of the while iteration A [i] should be replaced with A[ i-1 ]. so the comparison starts from the previous element.

CORRECTED insertionSort() METHOD

public void insertionSort()
{
int in, out;
for(out=1; out<nElems; out++)
{
long temp = a[out];
in = out;
while(in>0 && a[in-1] >= temp)
{
a[in] = a[in-1];
--in;
}
a[in] = temp;
}
}

COMPLETE CODE:

//ArrayIns.java

class ArrayIns{
private long[] a;
private int nElems;
public ArrayIns(int max)
{
a=new long[max];
nElems = 0;
}
public void insert(long value)
{
a[nElems] = value;
nElems++;
  
}
public void display()
{
for(int j=0; j<nElems; j++)
System.out.print(a[j]+" ");
System.out.print("\n");
}

/*********************************WRONG CODE*********************************/
// public void insertionSort()
// {
// int in, out;
// for(out=1; out<nElems; out++)
// {
// long temp = a[out];
// in = out;
// while(in>0 && a[in-1] >= temp)
// {
// a[in] = a[in-1];
// --in;
// }
// }
// }

/*********************************CORRECT CODE*********************************/
public void insertionSort()
{
int in, out;
for(out=1; out<nElems; out++)
{
long temp = a[out];
in = out;
while(in>0 && a[in-1] >= temp)
{
a[in] = a[in-1];
--in;
}
a[in] = temp;
}
}
}

//InsertionSortTest.java

class InsertionSortTest
{
public static void main (String[] args) {
int maxSize = 100;
ArrayIns arr;
arr = new ArrayIns(maxSize);
  
arr.insert(77);
arr.insert(99);
arr.insert(44);
arr.insert(55);
arr.insert(22);
arr.insert(88);
arr.insert(11);
arr.insert(00);
arr.insert(66);
arr.insert(33);
  
arr.display();
arr.insertionSort();
arr.display();
}
}

Above sample run:

1 class InsertionSortTest public static void main(String[] args) { int maxSize = 100; ArrayIns arr; arr = new ArrayIns(maxSiz

Add a comment
Know the answer?
Add Answer to:
// ArrayIns.java // demonstrates insertion sort 11--- class ArrayIns private long[] a; private int nElems; //...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Add a method called median() to the ArrayIns class in the insertSort.java program (Listing 3.3). This...

    Add a method called median() to the ArrayIns class in the insertSort.java program (Listing 3.3). This method should return the median value in the array. (Recall that in a group of numbers half are larger than the median and half are smaller.) Do it the easy way. LISTING 3.3 The insertSort.java Program // insertSort.java // demonstrates insertion sort // to run this program: C>java InsertSortApp //-------------------------------------------------------------- class ArrayIns { private long[] a; // ref to array a private int nElems;...

  • Another simple sort is the odd-even sort. The idea is to repeatedly make two passes through the a...

    Another simple sort is the odd-even sort. The idea is to repeatedly make two passes through the array. On the first pass you look at all the pairs of items, a[j] and a[j+1], where j is odd (j = 1, 3, 5, …). If their key values are out of order, you swap them. On the second pass you do the same for all the even values (j = 2, 4, 6, …). You do these two passes repeatedly until...

  • To the HighArray class in the highArray.java, add a method called getMax() that returns the value...

    To the HighArray class in the highArray.java, add a method called getMax() that returns the value of the highest key in the array, or -1 if the array is empty. Add some code in main() to exercise this method. You can assume all the keys are positive numbers. // highArray.java class HighArray { private long[] a; private int nElems;    public HighArray(int max)    { a = new long[max];    nElems = 0; } public boolean find(long searchKey) { int...

  • JAVA- Trace the recursive quick sort and partition methods in Lab6.java for this list of numbers:...

    JAVA- Trace the recursive quick sort and partition methods in Lab6.java for this list of numbers: 47 71 15 35 66 61 44 26 68 56 18 19 36 84 69 55 1. Find the value of pivot 2. Show the result after partitionIt() is called first time 3. Show the value of partition 4. Show the content of the array ///////////////////////////// Lab6.java class ArrayIns { private long[] theArray; // ref to array theArray private int nElems; // number of...

  • Copy the following java codes and compile //// HighArray.java //// HighArrayApp.java Study carefully the design and...

    Copy the following java codes and compile //// HighArray.java //// HighArrayApp.java Study carefully the design and implementation HighArray class and note the attributes and its methods.    Create findAll method which uses linear search algorithm to return all number of occurrences of specified element. /** * find an element from array and returns all number of occurrences of the specified element, returns 0 if the element does not exit. * * @param foundElement   Element to be found */ int findAll(int...

  • The following code is a Java code for insertion sort. I would like this code to...

    The following code is a Java code for insertion sort. I would like this code to be modified by not allowing more than 10 numbers of integer elements (if the user enters 11 or a higher number, an error should appear) and also finding the range of the elements after sorting. /* * Java Program to Implement Insertion Sort */ import java.util.Scanner; /* Class InsertionSort */ public class InsertionSortTwo { /* Insertion Sort function */ public static void sort( int...

  • I need to program 3 and add to program 2 bellows: Add the merge sort and...

    I need to program 3 and add to program 2 bellows: Add the merge sort and quick sort to program 2 and do the same timings, now with all 5 sorts and a 100,000 element array. Display the timing results from the sorts. DO NOT display the array. ____________________>>>>>>>>>>>>>>>>>>>>___________________________ (This is program 2 code that I did : ) ---->>>>>> code bellow from program 2 java program - Algorithms Write a program that randomly generates 100,000 integers into an array....

  • must provide the following public interface: public static void insertSort(int [] arr); public static void selectSort(int...

    must provide the following public interface: public static void insertSort(int [] arr); public static void selectSort(int [] arr); public static void quickSort(int [] arr); public static void mergeSort(int [] arr); The quick sort and merge sort must be implemented by using recursive thinking. So the students may provide the following private static methods: //merge method //merge two sorted portions of given array arr, namely, from start to middle //and from middle + 1 to end into one sorted portion, namely,...

  • 3. Modify the insertion sort algorithm discussed in class so that it can sort strings. That...

    3. Modify the insertion sort algorithm discussed in class so that it can sort strings. That is, its input will be an array, the type of which is string. The insertion sort algorithm will sort the elements in this array. Finally, its output will be a sorted array. As the solution of this problem, you need to submit the following answer(s): (1). The implementation of your algorithm in C# (20points). Algorithm: At each array-position, it checks the value there against...

  •   Given a bubble sort and the following array: [10,7,19,5,16] What are the values after the first...

      Given a bubble sort and the following array: [10,7,19,5,16] What are the values after the first iteration? Given an insertion sort and the following array: [50,5,35,70,6] What does the array look like after the first insertion:    Fill in the missing code for InsertionSort method // Insertion Sort method //sorts an array of Auto objects public static void insertionSort(Auto [] arr) { Auto temp; int j; for (int i=1; i<arr.length; i++) {     j=i;     temp=arr[i];                while (j!=0 &&(temp.getModel().compareTo(arr[[j-1].getModel())<0)...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT