There is a task for using BubbleSort algorythm: create array and add there 10000 numbers randomly. How to fix this method:
class Bubble{
public void randomInsert(long n) {
for(int j=0; j<nElems; j++)
n = (long)(java.lang.Math.random()*(nElems-1));
a[nElems] = n;
nElems++;
}}
To use here:
class NewBubbleApp {
public static void main(String[] args) {
int maxSize = 10000;
Bubble bubarr = new Bubble(maxSize);
bubarr.randomInsert();
bubarr.bubbleSort();
bubarr.display();
}}
Here's the whole code:
class Bubble{
private long[] a;
private int nElems;
public Bubble(int nElems) {
this.nElems = nElems;
a = new long[nElems];
nElems = 0;
}
public void randomInsert(long n) {
for(int j=0; j<nElems; j++)
n = (long)(java.lang.Math.random()*(nElems-1));
a[nElems] = n;
nElems++;}
public void display(){
for(int j=0; j<nElems; j++)
System.out.println(a[j] + " ");
System.out.println(" ");}
public void bubbleSort() {
int out, in;
for(out = nElems-1; out>1; out--)
for(in=0; in<out; in++)
if(a[in+1]<a[in])
swap(in, in+1);}
public void swap(int one, int two) {
long temp = a[one];
a[one] = a[two];
a[two] = temp;}}
class NewBubbleApp {
public static void main(String[] args) {
int maxSize = 10000;
Bubble bubarr = new Bubble(maxSize);
bubarr.randomInsert();
bubarr.bubbleSort();
bubarr.display();}}
Eclipse offers to use as: bubarr.randomInsert(maxSize). Anyone who had such this before? Help me, please
Aucun commentaire:
Enregistrer un commentaire