Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am trying to create a program that uses insertion sort and arrays to sort numb

ID: 3653154 • Letter: I

Question

I am trying to create a program that uses insertion sort and arrays to sort numbers inputted. Check the output below: Simple Sorting Program ====================== At start, list contains: [] Enter val (neg to end): 63 List now contains: [63] Enter val (neg to end): 4 List now contains: [4, 63] Enter val (neg to end): 77 List now contains: [4, 63, 77] Enter val (neg to end): 15 List now contains: [4, 15, 63, 77] Enter val (neg to end): 4 List now contains: [4, 4, 15, 63, 77] Enter val (neg to end): 9 List now contains: [4, 4, 9, 15, 63, 77] Enter val (neg to end): -1 At end, list contains: [4, 4, 9, 15, 63, 77] Goodbye! Thanks for your help.

Explanation / Answer

// insertSort.java // demonstrates insertion sort // to run this program: C>java InsertSortApp //-------------------------------------------------------------- class ArrayIns { private long[] a; // ref to array a private int nElems; // number of data items //-------------------------------------------------------------- public ArrayIns(int max) // constructor { a = new long[max]; // create the array nElems = 0; // no items yet } //-------------------------------------------------------------- public void insert(long value) // put element into array { a[nElems] = value; // insert it nElems++; // increment size } //-------------------------------------------------------------- public void display() // displays array contents { for(int j=0; j= temp) // until one is smaller, { a[in] = a[in-1]; // shift item to right --in; // go left one position } a[in] = temp; // insert marked item } // end for } // end insertionSort() //-------------------------------------------------------------- } // end class ArrayIns //////////////////////////////////////////////////////////////// class insertSort { 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); // insert 10 items 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(); // insertion-sort them arr.display(); // display them again } // end main() } // end class InsertSortApp ////////////////////////////////////////////////////////////////Here's the output from the insertSort.java program; it's the same
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote