In Java - Rewrite main() so it creates a large array and fills that array with d
ID: 3586093 • Letter: I
Question
In Java - Rewrite main() so it creates a large array and fills that array with data with random numbers. Insert 10,000 items, display the data before and after the sort. And add timing to see how long the sort takes.
// selectSort.java // demonstrates selection sort // to run this program: C>java SelectSortApp //////////////////////////////////////////////////////////////// class ArraySel { private long[] a; // ref to array a private int nElems; // number of data items // -------------------------------------------------------------- public ArraySel(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 < nElems; j++) // for each element, System.out.print(a[j] + " "); // display it System.out.println(""); } // -------------------------------------------------------------- public void selectionSort() { int out, in, min; for (out = 0; out < nElems - 1; out++) // outer loop { min = out; // minimum for (in = out + 1; in < nElems; in++) // inner loop if (a[in] < a[min]) // if min greater, min = in; // we have a new min swap(out, min); // swap them } // end for(out) } // end selectionSort() // -------------------------------------------------------------- private void swap(int one, int two) { long temp = a[one]; a[one] = a[two]; a[two] = temp; } // -------------------------------------------------------------- } // end class ArraySel //////////////////////////////////////////////////////////////// class SelectSortApp { public static void main(String[] args) { int maxSize = 100; // array size ArraySel arr; // reference to array arr = new ArraySel(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.selectionSort(); // selection-sort them arr.display(); // display them again } // end main() } // end class SelectSortApp ////////////////////////////////////////////////////////////////
Explanation / Answer
Please find my implementation.
public class ArraySel {
private long[] a; // ref to array a
private int nElems; // number of data items
// --------------------------------------------------------------
public ArraySel(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 < nElems; j++) // for each element,
System.out.print(a[j] + " "); // display it
System.out.println("");
}
// --------------------------------------------------------------
public void selectionSort() {
int out, in, min;
for (out = 0; out < nElems - 1; out++) // outer loop
{
min = out; // minimum
for (in = out + 1; in < nElems; in++) // inner loop
if (a[in] < a[min]) // if min greater,
min = in; // we have a new min
swap(out, min); // swap them
} // end for(out)
} // end selectionSort()
// --------------------------------------------------------------
private void swap(int one, int two) {
long temp = a[one];
a[one] = a[two];
a[two] = temp;
}
// --------------------------------------------------------------
} // end class ArraySel
////////////////////////////////////////////////////////////////
import java.util.Random;
public class SelectSortApp {
public static void main(String[] args) {
int maxSize = 10000; // array size
ArraySel arr; // reference to array
arr = new ArraySel(maxSize); // create the array
Random rand = new Random();
for(int i=0; i<maxSize; i++) {
arr.insert(rand.nextInt(1000)); // random number in range 1-999
}
arr.display(); // display items
System.out.println("------------------------------------------");
arr.selectionSort(); // selection-sort them
arr.display(); // display them again
} // end main()
} // end class SelectSortApp
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.