Hi everyone,
I'm trying to do a bubble sort where it takes the largest element and takes it to the right. It works for the most part, but then for some reason the right-most element (after the sorting) becomes 0 and then it acts accordingly. It happens sometimes with arrays, so I don't really know why that is. Here are my bubblesort, swapping, and display codes:
public void bubblesort() { int out,in; for(out=n;out>1;out--) { for(in=0;in<out;in++) { if(a[in]>a[in+1]) { swap(in,in+1); } display(); } } }
public void display() { for(int j=0;j<n;j++) { System.out.print(a[j] + " "); } System.out.println(); System.out.println(); }
public void swap(int one,int two) { long temp=a[one]; a[one]=a[two]; a[two]=temp; }