The bubbling sort has three record values, two of which point to two objects that need to be compared, one pointing to the end value of the numeric value that needs to be sorted.
In,in+1,out
Public classArraybub {Private Longa[]; Private intNelems; PublicArraybub (intmax) {a=New Long[Max]; Nelems=0; } Public voidInsertLongvalue) {A[nelems]=value; Nelems++; } Public voiddisplay () { for(intj=0;j<nelems;j++) {System.out.print (A[j]+" "); } System.out.println (); } //How to bubble sort Public voidBubblesort () {intOut,in;//The out record needs to sort the end value of the array for(out=nelems-1;out>1;out--) { //Compare the size of two objects (in and in+1) for(in=0;in<out;in++) { if(a[in]>a[in+1]) Swap (in, in+1); } } } //Exchange values for two objects Private voidSwapintOneintBoth ) { Longtemp=A[one]; A[one]=A[two]; A[two]=temp; } }
Public classTest { Public Static voidMain (string[] args) {intmaxsize=100; Arraybub Arraybub=NewArraybub (maxSize); Arraybub.insert (77); Arraybub.insert (99); Arraybub.insert (44); Arraybub.insert (55); Arraybub.insert (22); Arraybub.insert (88); Arraybub.insert (11); Arraybub.insert (0); Arraybub.insert (66); Arraybub.insert (33); Arraybub.display (); Arraybub.bubblesort (); Arraybub.display (); }}
Sort _ Simple Sort _ bubble sort