Home > Back-end >  Bubble sort
Bubble sort

Time:10-03

 
Private static void the bubbleSort (int [] arrays) {
Int len=arrays. Length;
Int temp.
for(int i=0; ifor(int j=0; jIf (arrays [j] & gt; The arrays [j + 1]) {
Temp=arrays [j + 1);
The arrays [j + 1)=arrays [j];
The arrays [j]=temp;
}
}
}
System. The out. Println (Arrays. ToString (Arrays));
}

Bubble sort is stable and should not see the arrays and arrays [j] [m + 1] will happen when equal exchange, so if I change the above code to the
 
If (arrays [j] & gt;=the arrays [j + 1]) {
Temp=arrays [j + 1);
The arrays [j + 1)=arrays [j];
The arrays [j]=temp;
}

In equal, still occur, so is stable?
Such thinking, this kind of sorting algorithm is stable and should not be determined according to the code? Why to see everyone in that bubble sort is stable?

CodePudding user response:

The second method is really unstable
However, the conventional thinking (that is, to discuss the stable condition) is different to exchange, the same will not exchange, so based on this premise, otherwise all sorts not stable), bubble is stable,
  • Related