For each cycle, the next two elements to compare the size, if the conditions to meet the exchange, the ultimate realization of the largest element to throw to the last side.
Prepared for 2 weeks of Java, C, C + +, data structure and algorithms, the written test was only a test overload and a single example plus 3 times shaking hands, this is an insult to me, more painful is actually brushed me down. Even a bubble-not-test recruitment is an incomplete recruitment.
public class Bubblesort {private int[] ParaL;
Public Bubblesort () {} is public void Show () {System.out.println ("The sorted record is:");
for (int para:paral) {System.out.print (para+ "");
} System.out.println ();
public void Bsort () {Boolean flagchange = false; int len = paral.length;//whether to Exchange flags for (int i = 0; i < len-1 i++) {////Total traverse n-1 times Flagchange = false;//No exchange for (int j = 0; J < Len-i-1;
J + +) {if (paral[j]>paral[j+1]) {Flagchange = true;
PARAL[J] = Paral[j] + paral[j+1];//two variable exchange paral[j+1] = Paral[j]-paral[j+1];
PARAL[J] = paral[j]-paral[j+1];
} if (!flagchange) {//If no break is exchanged;
}} public static void Main (string[] args) {int[] ParaL = {9,8,7,6,5,4,3,2,1};
Bubblesort qs = new Bubblesort ();
Qs.setparal (ParaL);
Qs.bsort ();
Qs.show ();
Int[] Getparal () {return paraL;
} public void Setparal (int[] paraL) {this.paral = ParaL; }
}
20160802 write again.
/**
* Classname:bubblesort
* @Description: Bubbling (traversing the previous n-i element, moving the largest element to the n-i position each time, which is roughly what it means)
* @author Panyk
* @date August 2, 2016 * * Public
class Bubblesort {public
int[] data;
public void sort () {
int temp;
Boolean change;
Show ();
for (int i=0; i<data.length-1;i++) {Change
= false;
for (int j=0; j<data.length-1-I; j + +) {
if (Data[j] > Data[j+1]) {
temp = data[j+1];
DATA[J+1] = data[j];
DATA[J] = temp;
Change = true;
}
}
Show ();
if (!change) {break
;
}}} private void Show () {for
(int temp:data) {
System.out.print (" " + temp);
}
System.out.println ();
}
public static void Main (string[] args) {
bubblesort bs = new Bubblesort ();
int[] data = new Int[]{1, 3, 5, 2, 4, 4, 1, 2};
Bs.data = data;
Bs.sort ();
}