冒泡排序法 Java
package
liaojiyong.net.blogjava;
/**
?
*
@author
liaojy
?
*
?
*/
public
class
BubbleSort {
???
static
int
[]
arr
= { 1, 2, 6, 3, 11, 92, 12, 5 };
???
/**
???
?
*
@param
args
???
?
*/
???
public
static
void
main(String[] args) {
??????
//
TODO
Auto-generated method stub
?????? System.
out
.println(
"Before Sort..."
);
??????
for
(
int
temp1 :
arr
) {
?????????? System.
out
.print(temp1 +
"? "
);
?????? }
?????? System.
out
.println();
??????
sort(
arr
);
?????? System.
out
.println(
"After Sort..."
);
??????
for
(
int
temp2 :
arr
) {
?????????? System.
out
.print(temp2 +
"? "
);
?????? }
?????? System.
out
.println();
??? }
???
public
static
void
sort(
int
[] a) {
??????
for
(
int
i = 0; i < a.
length
; i++) {
??????????
for
(
int
j = i + 1; j < a.
length
; j++) {
?????????????
if
(a[i] > a[j]) {
????????????????? a[i] = a[i] + a[j];
????????????????? a[j] = a[i] - a[j];
????????????????? a[i] = a[i] - a[j];
????????????? }
?????????? }
?????? }
??? }
}
Before Sort...
1? 2? 6? 3? 11? 92? 12? 5?
After Sort...
1? 2? 3? 5? 6? 11? 12? 92?
posted on 2007-03-19 15:01 liaojiyong 閱讀(3138) 評論(5) 編輯 收藏 所屬分類: Java