In Java - Rewrite main() so it creates a large array and fills that array with d
ID: 3586091 • 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. // bubbleSort.java // demonstrates bubble sort // to run this program: C>java BubbleSortApp //////////////////////////////////////////////////////////////// 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; j<nElems; j++) // for each element, System.out.print(a[j] + " "); // display it System.out.println(""); } // -------------------------------------------------------------- public void bubbleSort() { int out, in; for(out=nElems-1; out>1; out--) // outer loop (backward) for(in=0; in<out; in++) // inner loop (forward) if( a[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 //////////////////////////////////////////////////////////////// class BubbleSortApp { public static void main(String[] args) { int maxSize = 100; // array size ArrayBub arr; // reference to array arr = new ArrayBub(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.bubbleSort(); // bubble sort them arr.display(); // display them again } // end main() } // end class BubbleSortApp ////////////////////////////////////////////////////////////////
Explanation / Answer
Note : Could you please check the output .If you required any changes Just intimate.I will modify it.Thank You.
________________
ArrayBub.java
import java.util.Random;
class ArrayBub {
private int[] a; // ref to array a
private int nElems; // number of data items
// --------------------------------------------------------------
public ArrayBub(int max) // constructor
{
a = new int[max]; // create the array
nElems = 0; // no items yet
}
// --------------------------------------------------------------
public void insert(int 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 bubbleSort()
{
int temp;
for (int i = 0; i < nElems; i++)
{
for (int j = i + 1; j < nElems; j++)
{
if (a[i] > a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
} // end bubbleSort()
// --------------------------------------------------------------
public void generateArr() {
Random r = new Random();
for(int i=0;i<10000;i++)
{
insert(r.nextInt((100 - 1) + 1) + 1);
}
}
// --------------------------------------------------------------
} // end class ArrayBub
_________________
BubbleSortApp.java
import java.util.Random;
class BubbleSortApp {
public static void main(String[] args) {
int maxSize = 10000; // array size
ArrayBub arr; // reference to array
arr = new ArrayBub(maxSize); // create the array
arr.generateArr();
System.out.println("*** Before Sorting ***");
arr.display(); // display items
arr.bubbleSort(); // bubble sort them
System.out.println("*** After Sorting ***");
arr.display(); // display them again
} // end main()
} // end class BubbleSortApp
____________________
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.