Question: Java program: LABORATORY EXERCISE: 1. Write a program with the menu below. (Sorting with Integer type) Menu [1]Insert [2]Display [3]Sort [4]Exit Using an array of

Java program:

LABORATORY EXERCISE:

1. Write a program with the menu below. (Sorting with Integer type)

Menu

[1]Insert

[2]Display

[3]Sort

[4]Exit

Using an array of integers with a size of 10, the program will allow the user to insert numbers. Also, the program can display the content, then sort and re-sort the content of the array. The program should include error checking needed for the program.

2.

Write the algorithm and program fragment that will simulate the following sorting algorithms.

a. Selection Sort

b. Insertion Sort

using this class:

class ArrayBub { private long[] a; // ref to array a private int nElems; // number of data items //-------------------------------------------------------------- public ArrayBub(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; j0; out--) // outer loop (backward) for(in=0; in a[in+1] ) // out of order? swap(in, in+1); // swap them } // end bubbleSort() //-------------------------------------------------------------- private void swap(int one, int two) { long temp = a[one]; a[one] = a[two]; a[two] = temp; } //-------------------------------------------------------------- } // end class ArrayBub

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!