:)
Chcę aby posortował od najmniejszego elemntu w tablicy do największego.
Problem w tym że nie wiem jak to do końca działa bo mi sortuje nie wszystkie liczy które podałem do tablicy
oraz wypisuje po sortowaniu jedno 0.
class Test2{
private long[]a;
private int nElems;
public Test2(int max){
a = new long[max];
nElems = 0;
}
public void insert(long value){
a[nElems] = value;
nElems++;
}
public void display(){
for(int j=0; j<nElems; j++)
System.out.print(a[j] + ", ");
System.out.println("");
}
public void bubbleSort() {
for(int k=nElems--; k>0; k--)
for(int j=0; j<k; j++)
if( a[j] >a[j+1])
swap(j, j+1);
}
private void swap(int one, int two){
long temp =a[one];
a[one] = a[two];
a[two] = temp;
}
}
class Test{
public static void main(String args[]){
int maxSize =100;
Test2 p1;
p1 = new Test2(maxSize);
p1.insert(34);
p1.insert(77);
p1.insert(22);
p1.insert(47);
p1.insert(79);
p1.insert(42);
p1.insert(21);
p1.insert(41);
p1.insert(69);
p1.insert(12);
p1.display();
p1.bubbleSort();
p1.display();
}
}