您的位置:首页 > 其它

冒泡排序

2016-04-22 12:40 344 查看
package sun;

//从最右边开始,较小的元素一直往左边移动
//最左边的元素为最后的位置
public class Sort {
public static void bubble(int[] a) {
for (int i = 0; i < a.length; i++) {
for (int j = a.length - 1; j > i; j--) {
if (a[j] < a[j - 1]) {
int temp = a[j - 1];
a[j - 1] = a[j];
a[j] = temp;
}
}
}
}

public static void main(String[] args) {
int[] arr = { 9, 8,  4, 3, 2, 1 ,7, 6, 5,};
bubble(arr);
for (int a : arr)
System.out.println(a);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: