Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

image text in transcribed

image text in transcribed

image text in transcribed

// ArrayIns.java // demonstrates insertion sort 11--- class ArrayIns private long[] a; private int nElems; // ref to array a // number of data items public ArrayIns(int max) // constructor a = new long[max]; nElems - // create the array // no items yet --- public void insert(long value) // put element into array a[nElems] = value; nElems++; // insert it // increment size public void display() // displays array contents for(int j=0; j 88 afin-1) >= temp) // until one is smaller, ain) - afin-1]; // shift item to right // go left one position --in; } // end for // end insertionSort() } // -- > // end class ArrayIns V/ InsertSortTest.java // Test the insert sort algorithm -- class InsertSortTest public static void main(String[] args) { int maxSize = 100; // array size ArrayIns arr; // reference to array arr - new ArrayIns(maxSize); // create the array 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(); // display items arr.insertionSort(); // execute insertion sort arr.display(); // display items again } // end class InsertSortTest Description of the Problem: ArrayIns.java is the source code for the insertion sort algorithm, it is based on the process of repeatedly inserting a new element into already sorted list. At the (+1)th step, a[i] is inserted into its proper place among the already sorted a[0.1-1). Input: array A[O... n-1] Insertion Sort(A) For j=1 to length (A)-1 key= A[] i=i while i >0 and A[i]> key A[i]=A[i-1] i=i-1 A[i] = key Fig. 1 (a.) Analog of Insert Sort (6) Pseudo-code 1. What is the logic error of this program? Where it is? 2. Modify your input into {132, -32, 453, 1694, 342, 675, -582, -892, 643, 37), and submit your screenshot of the result by running the program

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions